bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU monitor |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3 | * |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 4 | * Copyright (c) 2003-2004 Fabrice Bellard |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5 | * |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 6 | * 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 Armbruster | e688df6 | 2018-02-01 12:18:31 +0100 | [diff] [blame] | 24 | |
Peter Maydell | d38ea87 | 2016-01-29 17:50:05 +0000 | [diff] [blame] | 25 | #include "qemu/osdep.h" |
Philippe Mathieu-Daudé | 8ec338a | 2018-06-25 09:42:34 -0300 | [diff] [blame] | 26 | #include "qemu/units.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 27 | #include <dirent.h> |
Paolo Bonzini | 33c1187 | 2016-03-15 16:58:45 +0100 | [diff] [blame] | 28 | #include "cpu.h" |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 29 | #include "hw/hw.h" |
Paolo Bonzini | b4a42f8 | 2013-02-04 11:37:52 +0100 | [diff] [blame] | 30 | #include "monitor/qdev.h" |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 31 | #include "hw/usb.h" |
Michael S. Tsirkin | a2cb15b | 2012-12-12 14:24:50 +0200 | [diff] [blame] | 32 | #include "hw/pci/pci.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 33 | #include "sysemu/watchdog.h" |
Gerd Hoffmann | 45a50b1 | 2009-10-01 16:42:33 +0200 | [diff] [blame] | 34 | #include "hw/loader.h" |
Paolo Bonzini | 022c62c | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 35 | #include "exec/gdbstub.h" |
Paolo Bonzini | 1422e32 | 2012-10-24 08:43:34 +0200 | [diff] [blame] | 36 | #include "net/net.h" |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 37 | #include "net/slirp.h" |
Marc-André Lureau | 4d43a60 | 2017-01-26 18:26:44 +0400 | [diff] [blame] | 38 | #include "chardev/char-fe.h" |
Peter Xu | a5ed352 | 2018-03-09 16:59:52 +0800 | [diff] [blame] | 39 | #include "chardev/char-io.h" |
Peter Xu | be933ff | 2018-03-26 14:38:56 +0800 | [diff] [blame] | 40 | #include "chardev/char-mux.h" |
Gerd Hoffmann | 7572150 | 2010-10-07 12:22:54 +0200 | [diff] [blame] | 41 | #include "ui/qemu-spice.h" |
Eduardo Habkost | e35704b | 2015-02-08 16:51:16 -0200 | [diff] [blame] | 42 | #include "sysemu/numa.h" |
Paolo Bonzini | 83c9089 | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 43 | #include "monitor/monitor.h" |
Marc-André Lureau | 213dcb0 | 2016-12-12 20:22:24 +0300 | [diff] [blame] | 44 | #include "qemu/config-file.h" |
Stefan Hajnoczi | 0150cd8 | 2013-11-14 11:54:15 +0100 | [diff] [blame] | 45 | #include "qemu/readline.h" |
Paolo Bonzini | 28ecbae | 2012-11-28 12:06:30 +0100 | [diff] [blame] | 46 | #include "ui/console.h" |
Gerd Hoffmann | c751a74 | 2013-12-04 15:02:28 +0100 | [diff] [blame] | 47 | #include "ui/input.h" |
Max Reitz | da27a00 | 2016-03-16 19:54:29 +0100 | [diff] [blame] | 48 | #include "sysemu/block-backend.h" |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 49 | #include "audio/audio.h" |
Paolo Bonzini | 76cad71 | 2012-10-24 11:12:21 +0200 | [diff] [blame] | 50 | #include "disas/disas.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 51 | #include "sysemu/balloon.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 52 | #include "qemu/timer.h" |
Vincent Palatin | b394662 | 2017-01-10 11:59:55 +0100 | [diff] [blame] | 53 | #include "sysemu/hw_accel.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 54 | #include "qemu/acl.h" |
Paolo Bonzini | bdee56f | 2013-04-02 18:28:41 +0200 | [diff] [blame] | 55 | #include "sysemu/tpm.h" |
Markus Armbruster | 452fcdb | 2018-02-01 12:18:39 +0100 | [diff] [blame] | 56 | #include "qapi/qmp/qdict.h" |
Markus Armbruster | cc7a8ea | 2015-03-17 17:22:46 +0100 | [diff] [blame] | 57 | #include "qapi/qmp/qerror.h" |
Markus Armbruster | 15280c3 | 2018-02-01 12:18:36 +0100 | [diff] [blame] | 58 | #include "qapi/qmp/qnum.h" |
Markus Armbruster | fc81fa1 | 2018-02-01 12:18:40 +0100 | [diff] [blame] | 59 | #include "qapi/qmp/qstring.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 60 | #include "qapi/qmp/qjson.h" |
| 61 | #include "qapi/qmp/json-streamer.h" |
| 62 | #include "qapi/qmp/json-parser.h" |
Peter Xu | 0213031 | 2018-03-09 16:59:53 +0800 | [diff] [blame] | 63 | #include "qapi/qmp/qlist.h" |
Markus Armbruster | a9c9427 | 2016-06-22 19:11:19 +0200 | [diff] [blame] | 64 | #include "qom/object_interfaces.h" |
Daniel P. Berrange | 0ab8ed1 | 2017-01-25 16:14:15 +0000 | [diff] [blame] | 65 | #include "trace-root.h" |
Lluís | 31965ae | 2011-08-31 20:31:24 +0200 | [diff] [blame] | 66 | #include "trace/control.h" |
Pavel Butsykin | bf95728 | 2015-09-10 18:38:59 +0300 | [diff] [blame] | 67 | #include "monitor/hmp-target.h" |
Lluís | 6d8a764 | 2011-08-31 20:30:43 +0200 | [diff] [blame] | 68 | #ifdef CONFIG_TRACE_SIMPLE |
Lluís | 31965ae | 2011-08-31 20:31:24 +0200 | [diff] [blame] | 69 | #include "trace/simple.h" |
Prerna Saxena | 22890ab | 2010-06-24 17:04:53 +0530 | [diff] [blame] | 70 | #endif |
Paolo Bonzini | 022c62c | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 71 | #include "exec/memory.h" |
Paolo Bonzini | 63c9155 | 2016-03-15 13:18:37 +0100 | [diff] [blame] | 72 | #include "exec/exec-all.h" |
Paolo Bonzini | 03dd024 | 2015-12-15 13:16:16 +0100 | [diff] [blame] | 73 | #include "qemu/log.h" |
Markus Armbruster | 922a01a | 2018-02-01 12:18:46 +0100 | [diff] [blame] | 74 | #include "qemu/option.h" |
Anthony Liguori | 48a32be | 2011-09-02 12:34:48 -0500 | [diff] [blame] | 75 | #include "hmp.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 76 | #include "qemu/thread.h" |
Hani Benhabiles | b21631f | 2014-05-27 23:39:37 +0100 | [diff] [blame] | 77 | #include "block/qapi.h" |
Markus Armbruster | eb815e2 | 2018-02-11 10:36:05 +0100 | [diff] [blame] | 78 | #include "qapi/qapi-commands.h" |
| 79 | #include "qapi/qapi-events.h" |
Markus Armbruster | e688df6 | 2018-02-01 12:18:31 +0100 | [diff] [blame] | 80 | #include "qapi/error.h" |
Wenchao Xia | 43a14cf | 2014-06-18 08:43:31 +0200 | [diff] [blame] | 81 | #include "qapi/qmp-event.h" |
Markus Armbruster | eb815e2 | 2018-02-11 10:36:05 +0100 | [diff] [blame] | 82 | #include "qapi/qapi-introspect.h" |
Alberto Garcia | dc59997 | 2016-03-10 13:55:26 +0200 | [diff] [blame] | 83 | #include "sysemu/qtest.h" |
Paolo Bonzini | d2528bd | 2017-03-03 12:01:16 +0100 | [diff] [blame] | 84 | #include "sysemu/cpus.h" |
Peter Xu | a5ed352 | 2018-03-09 16:59:52 +0800 | [diff] [blame] | 85 | #include "sysemu/iothread.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 86 | #include "qemu/cutils.h" |
ths | 6a5bd30 | 2007-12-03 17:05:38 +0000 | [diff] [blame] | 87 | |
Jason J. Herne | a4538a5 | 2015-06-26 14:07:21 -0400 | [diff] [blame] | 88 | #if defined(TARGET_S390X) |
| 89 | #include "hw/s390x/storage-keys.h" |
Claudio Imbrenda | f860d49 | 2016-08-15 18:44:04 +0200 | [diff] [blame] | 90 | #include "hw/s390x/storage-attributes.h" |
Jason J. Herne | a4538a5 | 2015-06-26 14:07:21 -0400 | [diff] [blame] | 91 | #endif |
| 92 | |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 93 | /* |
| 94 | * Supported types: |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 95 | * |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 96 | * 'F' filename |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 97 | * 'B' block device name |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 98 | * 's' string (accept optional quote) |
Wenchao Xia | 129be00 | 2013-08-27 20:38:26 +0800 | [diff] [blame] | 99 | * 'S' it just appends the rest of the string (accept optional quote) |
Markus Armbruster | 361127d | 2010-02-10 20:24:35 +0100 | [diff] [blame] | 100 | * 'O' option string of the form NAME=VALUE,... |
| 101 | * parsed according to QemuOptsList given by its name |
| 102 | * Example: 'device:O' uses qemu_device_opts. |
| 103 | * Restriction: only lists with empty desc are supported |
| 104 | * TODO lift the restriction |
bellard | 92a31b1 | 2005-02-10 22:00:52 +0000 | [diff] [blame] | 105 | * 'i' 32 bit integer |
| 106 | * 'l' target long (32 or 64 bit) |
Luiz Capitulino | 9116284 | 2012-04-26 17:34:30 -0300 | [diff] [blame] | 107 | * 'M' Non-negative target long (32 or 64 bit), in user mode the |
| 108 | * value is multiplied by 2^20 (think Mebibyte) |
Jes Sorensen | dbc0c67 | 2010-10-21 17:15:47 +0200 | [diff] [blame] | 109 | * 'o' octets (aka bytes) |
Kevin Wolf | 5e00984 | 2013-06-05 14:19:27 +0200 | [diff] [blame] | 110 | * user mode accepts an optional E, e, P, p, T, t, G, g, M, m, |
| 111 | * K, k suffix, which multiplies the value by 2^60 for suffixes E |
| 112 | * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t, |
| 113 | * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k |
Markus Armbruster | fccfb11e | 2010-01-25 14:23:06 +0100 | [diff] [blame] | 114 | * 'T' double |
| 115 | * user mode accepts an optional ms, us, ns suffix, |
| 116 | * which divides the value by 1e3, 1e6, 1e9, respectively |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 117 | * '/' optional gdb-like print format (like "/10x") |
| 118 | * |
Luiz Capitulino | fb46660 | 2009-08-28 15:27:27 -0300 | [diff] [blame] | 119 | * '?' optional type (for all types, except '/') |
| 120 | * '.' other form of optional type (for 'i' and 'l') |
Markus Armbruster | 942cd1f | 2010-03-26 09:07:09 +0100 | [diff] [blame] | 121 | * 'b' boolean |
| 122 | * user mode accepts "on" or "off" |
Luiz Capitulino | fb46660 | 2009-08-28 15:27:27 -0300 | [diff] [blame] | 123 | * '-' optional parameter (eg. '-f') |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 124 | * |
| 125 | */ |
| 126 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 127 | typedef struct mon_cmd_t { |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 128 | const char *name; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 129 | const char *args_type; |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 130 | const char *params; |
| 131 | const char *help; |
Dr. David Alan Gilbert | c3120f7 | 2018-06-20 16:39:41 +0100 | [diff] [blame] | 132 | const char *flags; /* p=preconfig */ |
Marc-André Lureau | 2b9e357 | 2016-09-12 13:19:06 +0400 | [diff] [blame] | 133 | void (*cmd)(Monitor *mon, const QDict *qdict); |
| 134 | /* @sub_table is a list of 2nd level of commands. If it does not exist, |
| 135 | * cmd should be used. If it exists, sub_table[?].cmd should be |
| 136 | * used, and cmd of 1st level plays the role of help function. |
Wenchao Xia | 5f3d335 | 2013-01-14 14:06:27 +0800 | [diff] [blame] | 137 | */ |
| 138 | struct mon_cmd_t *sub_table; |
Hani Benhabiles | bfa40f7 | 2014-04-13 16:25:06 +0100 | [diff] [blame] | 139 | void (*command_completion)(ReadLineState *rs, int nb_args, const char *str); |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 140 | } mon_cmd_t; |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 141 | |
Mark McLoughlin | f07918f | 2009-07-22 09:11:40 +0100 | [diff] [blame] | 142 | /* file descriptors passed via SCM_RIGHTS */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 143 | typedef struct mon_fd_t mon_fd_t; |
| 144 | struct mon_fd_t { |
Mark McLoughlin | f07918f | 2009-07-22 09:11:40 +0100 | [diff] [blame] | 145 | char *name; |
| 146 | int fd; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 147 | QLIST_ENTRY(mon_fd_t) next; |
Mark McLoughlin | f07918f | 2009-07-22 09:11:40 +0100 | [diff] [blame] | 148 | }; |
| 149 | |
Corey Bryant | ba1c048 | 2012-08-14 16:43:43 -0400 | [diff] [blame] | 150 | /* file descriptor associated with a file descriptor set */ |
| 151 | typedef struct MonFdsetFd MonFdsetFd; |
| 152 | struct MonFdsetFd { |
| 153 | int fd; |
| 154 | bool removed; |
| 155 | char *opaque; |
| 156 | QLIST_ENTRY(MonFdsetFd) next; |
| 157 | }; |
| 158 | |
| 159 | /* file descriptor set containing fds passed via SCM_RIGHTS */ |
| 160 | typedef struct MonFdset MonFdset; |
| 161 | struct MonFdset { |
| 162 | int64_t id; |
| 163 | QLIST_HEAD(, MonFdsetFd) fds; |
Corey Bryant | adb696f | 2012-08-14 16:43:47 -0400 | [diff] [blame] | 164 | QLIST_HEAD(, MonFdsetFd) dup_fds; |
Corey Bryant | ba1c048 | 2012-08-14 16:43:43 -0400 | [diff] [blame] | 165 | QLIST_ENTRY(MonFdset) next; |
| 166 | }; |
| 167 | |
Markus Armbruster | 74358f2 | 2015-03-06 19:35:59 +0100 | [diff] [blame] | 168 | typedef struct { |
Luiz Capitulino | 5fa737a | 2009-11-26 22:59:01 -0200 | [diff] [blame] | 169 | JSONMessageParser parser; |
Markus Armbruster | f994b25 | 2015-03-06 19:51:51 +0100 | [diff] [blame] | 170 | /* |
| 171 | * When a client connects, we're in capabilities negotiation mode. |
Markus Armbruster | 774a6b6 | 2018-07-03 10:53:57 +0200 | [diff] [blame] | 172 | * @commands is &qmp_cap_negotiation_commands then. When command |
| 173 | * qmp_capabilities succeeds, we go into command mode, and |
| 174 | * @command becomes &qmp_commands. |
Markus Armbruster | f994b25 | 2015-03-06 19:51:51 +0100 | [diff] [blame] | 175 | */ |
Markus Armbruster | 635db18 | 2017-03-03 13:32:27 +0100 | [diff] [blame] | 176 | QmpCommandList *commands; |
Markus Armbruster | 279f9e0 | 2018-07-03 10:53:56 +0200 | [diff] [blame] | 177 | bool capab_offered[QMP_CAPABILITY__MAX]; /* capabilities offered */ |
| 178 | bool capab[QMP_CAPABILITY__MAX]; /* offered and accepted */ |
Peter Xu | 71da466 | 2018-03-09 16:59:57 +0800 | [diff] [blame] | 179 | /* |
Markus Armbruster | 774a6b6 | 2018-07-03 10:53:57 +0200 | [diff] [blame] | 180 | * Protects qmp request/response queue. |
| 181 | * Take monitor_lock first when you need both. |
Peter Xu | 71da466 | 2018-03-09 16:59:57 +0800 | [diff] [blame] | 182 | */ |
| 183 | QemuMutex qmp_queue_lock; |
| 184 | /* Input queue that holds all the parsed QMP requests */ |
| 185 | GQueue *qmp_requests; |
Peter Xu | abe3cd0 | 2018-03-09 17:00:02 +0800 | [diff] [blame] | 186 | /* Output queue contains all the QMP responses in order */ |
| 187 | GQueue *qmp_responses; |
Markus Armbruster | 74358f2 | 2015-03-06 19:35:59 +0100 | [diff] [blame] | 188 | } MonitorQMP; |
Luiz Capitulino | 5fa737a | 2009-11-26 22:59:01 -0200 | [diff] [blame] | 189 | |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 190 | /* |
| 191 | * To prevent flooding clients, events can be throttled. The |
| 192 | * throttling is calculated globally, rather than per-Monitor |
| 193 | * instance. |
| 194 | */ |
Wenchao Xia | 43a14cf | 2014-06-18 08:43:31 +0200 | [diff] [blame] | 195 | typedef struct MonitorQAPIEventState { |
Markus Armbruster | 7de0be6 | 2015-10-15 17:08:35 +0200 | [diff] [blame] | 196 | QAPIEvent event; /* Throttling state for this event type and... */ |
| 197 | QDict *data; /* ... data, see qapi_event_throttle_equal() */ |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 198 | QEMUTimer *timer; /* Timer for handling delayed events */ |
Markus Armbruster | 688b4b7 | 2015-10-15 17:08:30 +0200 | [diff] [blame] | 199 | QDict *qdict; /* Delayed event (if any) */ |
Wenchao Xia | 43a14cf | 2014-06-18 08:43:31 +0200 | [diff] [blame] | 200 | } MonitorQAPIEventState; |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 201 | |
Markus Armbruster | b9b03ab | 2015-10-15 17:08:33 +0200 | [diff] [blame] | 202 | typedef struct { |
| 203 | int64_t rate; /* Minimum time (in ns) between two events */ |
| 204 | } MonitorQAPIEventConf; |
| 205 | |
aliguori | 8712716 | 2009-03-05 23:01:29 +0000 | [diff] [blame] | 206 | struct Monitor { |
Marc-André Lureau | 32a6ebe | 2016-10-22 12:52:52 +0300 | [diff] [blame] | 207 | CharBackend chr; |
Gerd Hoffmann | a7aec5d | 2009-09-10 10:58:54 +0200 | [diff] [blame] | 208 | int reset_seen; |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 209 | int flags; |
Peter Xu | df152fb | 2018-03-09 16:59:55 +0800 | [diff] [blame] | 210 | int suspend_cnt; /* Needs to be accessed atomically */ |
Luiz Capitulino | 48c043d | 2013-04-02 15:07:33 -0400 | [diff] [blame] | 211 | bool skip_flush; |
Markus Armbruster | f91dc2a | 2018-07-03 10:53:45 +0200 | [diff] [blame] | 212 | bool use_io_thread; |
Peter Xu | d9f2528 | 2018-06-08 11:55:07 +0800 | [diff] [blame] | 213 | |
| 214 | /* |
| 215 | * State used only in the thread "owning" the monitor. |
Markus Armbruster | cab5ad8 | 2018-07-03 10:53:46 +0200 | [diff] [blame] | 216 | * If @use_io_thread, this is @mon_iothread. |
Peter Xu | d9f2528 | 2018-06-08 11:55:07 +0800 | [diff] [blame] | 217 | * Else, it's the main thread. |
| 218 | * These members can be safely accessed without locks. |
| 219 | */ |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 220 | ReadLineState *rs; |
Peter Xu | d9f2528 | 2018-06-08 11:55:07 +0800 | [diff] [blame] | 221 | |
Markus Armbruster | 74358f2 | 2015-03-06 19:35:59 +0100 | [diff] [blame] | 222 | MonitorQMP qmp; |
Greg Kurz | 751f8cf | 2017-10-17 10:16:22 +0200 | [diff] [blame] | 223 | gchar *mon_cpu_path; |
Markus Armbruster | 097310b | 2014-10-07 13:59:15 +0200 | [diff] [blame] | 224 | BlockCompletionFunc *password_completion_cb; |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 225 | void *password_opaque; |
Wenchao Xia | 7717239 | 2013-08-27 20:38:20 +0800 | [diff] [blame] | 226 | mon_cmd_t *cmd_table; |
Peter Xu | 238d9f3 | 2018-03-09 16:59:51 +0800 | [diff] [blame] | 227 | QTAILQ_ENTRY(Monitor) entry; |
Peter Xu | dc7cbcd | 2018-06-08 11:55:05 +0800 | [diff] [blame] | 228 | |
| 229 | /* |
| 230 | * The per-monitor lock. We can't access guest memory when holding |
| 231 | * the lock. |
| 232 | */ |
| 233 | QemuMutex mon_lock; |
| 234 | |
| 235 | /* |
Markus Armbruster | 774a6b6 | 2018-07-03 10:53:57 +0200 | [diff] [blame] | 236 | * Members that are protected by the per-monitor lock |
Peter Xu | dc7cbcd | 2018-06-08 11:55:05 +0800 | [diff] [blame] | 237 | */ |
Peter Xu | 9409fc0 | 2018-06-08 11:55:06 +0800 | [diff] [blame] | 238 | QLIST_HEAD(, mon_fd_t) fds; |
Peter Xu | dc7cbcd | 2018-06-08 11:55:05 +0800 | [diff] [blame] | 239 | QString *outbuf; |
| 240 | guint out_watch; |
| 241 | /* Read under either BQL or mon_lock, written with BQL+mon_lock. */ |
| 242 | int mux_out; |
aliguori | 8712716 | 2009-03-05 23:01:29 +0000 | [diff] [blame] | 243 | }; |
| 244 | |
Markus Armbruster | 774a6b6 | 2018-07-03 10:53:57 +0200 | [diff] [blame] | 245 | /* Shared monitor I/O thread */ |
Markus Armbruster | cab5ad8 | 2018-07-03 10:53:46 +0200 | [diff] [blame] | 246 | IOThread *mon_iothread; |
| 247 | |
| 248 | /* Bottom half to dispatch the requests received from I/O thread */ |
| 249 | QEMUBH *qmp_dispatcher_bh; |
| 250 | |
| 251 | /* Bottom half to deliver the responses back to clients */ |
| 252 | QEMUBH *qmp_respond_bh; |
Peter Xu | a5ed352 | 2018-03-09 16:59:52 +0800 | [diff] [blame] | 253 | |
Peter Xu | 6d2d563 | 2018-03-26 14:38:55 +0800 | [diff] [blame] | 254 | struct QMPRequest { |
| 255 | /* Owner of the request */ |
| 256 | Monitor *mon; |
| 257 | /* "id" field of the request */ |
| 258 | QObject *id; |
Markus Armbruster | 1cc3747 | 2018-07-03 10:53:44 +0200 | [diff] [blame] | 259 | /* |
| 260 | * Request object to be handled or Error to be reported |
| 261 | * (exactly one of them is non-null) |
| 262 | */ |
Peter Xu | 6d2d563 | 2018-03-26 14:38:55 +0800 | [diff] [blame] | 263 | QObject *req; |
Markus Armbruster | 1cc3747 | 2018-07-03 10:53:44 +0200 | [diff] [blame] | 264 | Error *err; |
Peter Xu | 6d2d563 | 2018-03-26 14:38:55 +0800 | [diff] [blame] | 265 | /* |
| 266 | * Whether we need to resume the monitor afterward. This flag is |
| 267 | * used to emulate the old QMP server behavior that the current |
| 268 | * command must be completed before execution of the next one. |
| 269 | */ |
| 270 | bool need_resume; |
| 271 | }; |
| 272 | typedef struct QMPRequest QMPRequest; |
| 273 | |
Luiz Capitulino | 2dbc8db | 2010-05-26 16:13:09 -0300 | [diff] [blame] | 274 | /* QMP checker flags */ |
| 275 | #define QMP_ACCEPT_UNKNOWNS 1 |
| 276 | |
Peter Xu | 095cb1b | 2018-06-08 11:55:08 +0800 | [diff] [blame] | 277 | /* Protects mon_list, monitor_qapi_event_state. */ |
Paolo Bonzini | d622cb5 | 2014-06-18 08:44:00 +0200 | [diff] [blame] | 278 | static QemuMutex monitor_lock; |
Peter Xu | 095cb1b | 2018-06-08 11:55:08 +0800 | [diff] [blame] | 279 | static GHashTable *monitor_qapi_event_state; |
Peter Xu | 238d9f3 | 2018-03-09 16:59:51 +0800 | [diff] [blame] | 280 | static QTAILQ_HEAD(mon_list, Monitor) mon_list; |
Peter Xu | 095cb1b | 2018-06-08 11:55:08 +0800 | [diff] [blame] | 281 | |
Peter Xu | 4745146 | 2018-06-08 11:55:11 +0800 | [diff] [blame] | 282 | /* Protects mon_fdsets */ |
| 283 | static QemuMutex mon_fdsets_lock; |
Corey Bryant | ba1c048 | 2012-08-14 16:43:43 -0400 | [diff] [blame] | 284 | static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets; |
Peter Xu | 4745146 | 2018-06-08 11:55:11 +0800 | [diff] [blame] | 285 | |
Corey Bryant | efb87c1 | 2012-08-14 16:43:48 -0400 | [diff] [blame] | 286 | static int mon_refcount; |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 287 | |
Wayne Xia | 816f892 | 2011-10-12 11:32:41 +0800 | [diff] [blame] | 288 | static mon_cmd_t mon_cmds[]; |
| 289 | static mon_cmd_t info_cmds[]; |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 290 | |
Markus Armbruster | 635db18 | 2017-03-03 13:32:27 +0100 | [diff] [blame] | 291 | QmpCommandList qmp_commands, qmp_cap_negotiation_commands; |
Markus Armbruster | 1527bad | 2017-03-03 13:32:25 +0100 | [diff] [blame] | 292 | |
Peter Xu | 62aa1d8 | 2018-07-20 11:34:51 +0800 | [diff] [blame] | 293 | __thread Monitor *cur_mon; |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 294 | |
Stefan Hajnoczi | c60bf33 | 2013-11-14 11:54:14 +0100 | [diff] [blame] | 295 | static void monitor_command_cb(void *opaque, const char *cmdline, |
| 296 | void *readline_opaque); |
aliguori | 83ab795 | 2008-08-19 14:44:22 +0000 | [diff] [blame] | 297 | |
Markus Armbruster | 9f3982f | 2015-03-06 19:56:38 +0100 | [diff] [blame] | 298 | /** |
| 299 | * Is @mon a QMP monitor? |
| 300 | */ |
| 301 | static inline bool monitor_is_qmp(const Monitor *mon) |
Luiz Capitulino | 418173c | 2009-11-26 22:58:51 -0200 | [diff] [blame] | 302 | { |
| 303 | return (mon->flags & MONITOR_USE_CONTROL); |
| 304 | } |
| 305 | |
Markus Armbruster | 489653b | 2015-03-06 20:01:05 +0100 | [diff] [blame] | 306 | /** |
Markus Armbruster | 774a6b6 | 2018-07-03 10:53:57 +0200 | [diff] [blame] | 307 | * Is @mon is using readline? |
| 308 | * Note: not all HMP monitors use readline, e.g., gdbserver has a |
| 309 | * non-interactive HMP monitor, so readline is not used there. |
Peter Xu | e3e977d | 2018-03-09 16:59:56 +0800 | [diff] [blame] | 310 | */ |
| 311 | static inline bool monitor_uses_readline(const Monitor *mon) |
| 312 | { |
| 313 | return mon->flags & MONITOR_USE_READLINE; |
| 314 | } |
| 315 | |
| 316 | static inline bool monitor_is_hmp_non_interactive(const Monitor *mon) |
| 317 | { |
| 318 | return !monitor_is_qmp(mon) && !monitor_uses_readline(mon); |
| 319 | } |
| 320 | |
Peter Xu | 6e8c5f4 | 2018-06-08 11:55:09 +0800 | [diff] [blame] | 321 | /* |
| 322 | * Return the clock to use for recording an event's time. |
Markus Armbruster | 774a6b6 | 2018-07-03 10:53:57 +0200 | [diff] [blame] | 323 | * It's QEMU_CLOCK_REALTIME, except for qtests it's |
| 324 | * QEMU_CLOCK_VIRTUAL, to support testing rate limits. |
Peter Xu | 6e8c5f4 | 2018-06-08 11:55:09 +0800 | [diff] [blame] | 325 | * Beware: result is invalid before configure_accelerator(). |
| 326 | */ |
| 327 | static inline QEMUClockType monitor_get_event_clock(void) |
| 328 | { |
Peter Xu | 6e8c5f4 | 2018-06-08 11:55:09 +0800 | [diff] [blame] | 329 | return qtest_enabled() ? QEMU_CLOCK_VIRTUAL : QEMU_CLOCK_REALTIME; |
| 330 | } |
| 331 | |
Peter Xu | e3e977d | 2018-03-09 16:59:56 +0800 | [diff] [blame] | 332 | /** |
Markus Armbruster | 489653b | 2015-03-06 20:01:05 +0100 | [diff] [blame] | 333 | * Is the current monitor, if any, a QMP monitor? |
| 334 | */ |
| 335 | bool monitor_cur_is_qmp(void) |
Markus Armbruster | 6620d3c | 2010-02-11 17:05:43 +0100 | [diff] [blame] | 336 | { |
Markus Armbruster | 9f3982f | 2015-03-06 19:56:38 +0100 | [diff] [blame] | 337 | return cur_mon && monitor_is_qmp(cur_mon); |
Markus Armbruster | 6620d3c | 2010-02-11 17:05:43 +0100 | [diff] [blame] | 338 | } |
| 339 | |
Anthony Liguori | 7060b47 | 2011-09-02 12:34:50 -0500 | [diff] [blame] | 340 | void monitor_read_command(Monitor *mon, int show_prompt) |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 341 | { |
Luiz Capitulino | 183e6e5 | 2009-12-14 18:53:23 -0200 | [diff] [blame] | 342 | if (!mon->rs) |
| 343 | return; |
| 344 | |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 345 | readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL); |
| 346 | if (show_prompt) |
| 347 | readline_show_prompt(mon->rs); |
| 348 | } |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 349 | |
Anthony Liguori | 7060b47 | 2011-09-02 12:34:50 -0500 | [diff] [blame] | 350 | int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func, |
| 351 | void *opaque) |
aliguori | bb5fc20 | 2009-03-05 23:01:15 +0000 | [diff] [blame] | 352 | { |
Markus Armbruster | bcf5d19 | 2015-03-12 17:26:46 +0100 | [diff] [blame] | 353 | if (mon->rs) { |
aliguori | cde76ee | 2009-03-05 23:01:51 +0000 | [diff] [blame] | 354 | readline_start(mon->rs, "Password: ", 1, readline_func, opaque); |
| 355 | /* prompt is printed on return from the command handler */ |
| 356 | return 0; |
| 357 | } else { |
| 358 | monitor_printf(mon, "terminal does not support password prompting\n"); |
| 359 | return -ENOTTY; |
| 360 | } |
aliguori | bb5fc20 | 2009-03-05 23:01:15 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Peter Xu | 6d2d563 | 2018-03-26 14:38:55 +0800 | [diff] [blame] | 363 | static void qmp_request_free(QMPRequest *req) |
| 364 | { |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 365 | qobject_unref(req->id); |
| 366 | qobject_unref(req->req); |
Markus Armbruster | 1cc3747 | 2018-07-03 10:53:44 +0200 | [diff] [blame] | 367 | error_free(req->err); |
Peter Xu | 6d2d563 | 2018-03-26 14:38:55 +0800 | [diff] [blame] | 368 | g_free(req); |
| 369 | } |
| 370 | |
Markus Armbruster | 774a6b6 | 2018-07-03 10:53:57 +0200 | [diff] [blame] | 371 | /* Caller must hold mon->qmp.qmp_queue_lock */ |
Peter Xu | 6d2d563 | 2018-03-26 14:38:55 +0800 | [diff] [blame] | 372 | static void monitor_qmp_cleanup_req_queue_locked(Monitor *mon) |
| 373 | { |
| 374 | while (!g_queue_is_empty(mon->qmp.qmp_requests)) { |
| 375 | qmp_request_free(g_queue_pop_head(mon->qmp.qmp_requests)); |
| 376 | } |
| 377 | } |
| 378 | |
Markus Armbruster | 774a6b6 | 2018-07-03 10:53:57 +0200 | [diff] [blame] | 379 | /* Caller must hold the mon->qmp.qmp_queue_lock */ |
Peter Xu | 6d2d563 | 2018-03-26 14:38:55 +0800 | [diff] [blame] | 380 | static void monitor_qmp_cleanup_resp_queue_locked(Monitor *mon) |
| 381 | { |
| 382 | while (!g_queue_is_empty(mon->qmp.qmp_responses)) { |
Markus Armbruster | d43b169 | 2018-07-03 10:53:49 +0200 | [diff] [blame] | 383 | qobject_unref((QDict *)g_queue_pop_head(mon->qmp.qmp_responses)); |
Peter Xu | 6d2d563 | 2018-03-26 14:38:55 +0800 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | |
| 387 | static void monitor_qmp_cleanup_queues(Monitor *mon) |
| 388 | { |
| 389 | qemu_mutex_lock(&mon->qmp.qmp_queue_lock); |
| 390 | monitor_qmp_cleanup_req_queue_locked(mon); |
| 391 | monitor_qmp_cleanup_resp_queue_locked(mon); |
| 392 | qemu_mutex_unlock(&mon->qmp.qmp_queue_lock); |
| 393 | } |
| 394 | |
| 395 | |
Paolo Bonzini | 6cff3e8 | 2014-06-18 08:43:59 +0200 | [diff] [blame] | 396 | static void monitor_flush_locked(Monitor *mon); |
| 397 | |
Gerd Hoffmann | f628926 | 2013-03-19 10:57:56 +0100 | [diff] [blame] | 398 | static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond, |
| 399 | void *opaque) |
| 400 | { |
Laszlo Ersek | 293d2a0 | 2013-07-16 20:19:41 +0200 | [diff] [blame] | 401 | Monitor *mon = opaque; |
| 402 | |
Peter Xu | dc7cbcd | 2018-06-08 11:55:05 +0800 | [diff] [blame] | 403 | qemu_mutex_lock(&mon->mon_lock); |
Paolo Bonzini | 6cff3e8 | 2014-06-18 08:43:59 +0200 | [diff] [blame] | 404 | mon->out_watch = 0; |
| 405 | monitor_flush_locked(mon); |
Peter Xu | dc7cbcd | 2018-06-08 11:55:05 +0800 | [diff] [blame] | 406 | qemu_mutex_unlock(&mon->mon_lock); |
Gerd Hoffmann | f628926 | 2013-03-19 10:57:56 +0100 | [diff] [blame] | 407 | return FALSE; |
| 408 | } |
| 409 | |
Markus Armbruster | 774a6b6 | 2018-07-03 10:53:57 +0200 | [diff] [blame] | 410 | /* Caller must hold mon->mon_lock */ |
Paolo Bonzini | 6cff3e8 | 2014-06-18 08:43:59 +0200 | [diff] [blame] | 411 | static void monitor_flush_locked(Monitor *mon) |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 412 | { |
Gerd Hoffmann | f628926 | 2013-03-19 10:57:56 +0100 | [diff] [blame] | 413 | int rc; |
Luiz Capitulino | e1f2641 | 2013-03-25 13:52:26 -0400 | [diff] [blame] | 414 | size_t len; |
| 415 | const char *buf; |
Gerd Hoffmann | f628926 | 2013-03-19 10:57:56 +0100 | [diff] [blame] | 416 | |
Luiz Capitulino | 48c043d | 2013-04-02 15:07:33 -0400 | [diff] [blame] | 417 | if (mon->skip_flush) { |
| 418 | return; |
| 419 | } |
| 420 | |
Luiz Capitulino | e1f2641 | 2013-03-25 13:52:26 -0400 | [diff] [blame] | 421 | buf = qstring_get_str(mon->outbuf); |
| 422 | len = qstring_get_length(mon->outbuf); |
| 423 | |
Paolo Bonzini | a4cc73d | 2013-05-31 14:00:27 +0200 | [diff] [blame] | 424 | if (len && !mon->mux_out) { |
Marc-André Lureau | 5345fdb | 2016-10-22 12:52:55 +0300 | [diff] [blame] | 425 | rc = qemu_chr_fe_write(&mon->chr, (const uint8_t *) buf, len); |
Stratos Psomadakis | 056f49f | 2014-01-27 12:30:15 +0200 | [diff] [blame] | 426 | if ((rc < 0 && errno != EAGAIN) || (rc == len)) { |
| 427 | /* all flushed or error */ |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 428 | qobject_unref(mon->outbuf); |
Luiz Capitulino | e1f2641 | 2013-03-25 13:52:26 -0400 | [diff] [blame] | 429 | mon->outbuf = qstring_new(); |
Gerd Hoffmann | f628926 | 2013-03-19 10:57:56 +0100 | [diff] [blame] | 430 | return; |
| 431 | } |
| 432 | if (rc > 0) { |
Eric Blake | 3b7c78c | 2016-05-17 16:00:15 -0600 | [diff] [blame] | 433 | /* partial write */ |
Luiz Capitulino | e1f2641 | 2013-03-25 13:52:26 -0400 | [diff] [blame] | 434 | QString *tmp = qstring_from_str(buf + rc); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 435 | qobject_unref(mon->outbuf); |
Luiz Capitulino | e1f2641 | 2013-03-25 13:52:26 -0400 | [diff] [blame] | 436 | mon->outbuf = tmp; |
Gerd Hoffmann | f628926 | 2013-03-19 10:57:56 +0100 | [diff] [blame] | 437 | } |
Paolo Bonzini | 6cff3e8 | 2014-06-18 08:43:59 +0200 | [diff] [blame] | 438 | if (mon->out_watch == 0) { |
Marc-André Lureau | 32a6ebe | 2016-10-22 12:52:52 +0300 | [diff] [blame] | 439 | mon->out_watch = |
Marc-André Lureau | 5345fdb | 2016-10-22 12:52:55 +0300 | [diff] [blame] | 440 | qemu_chr_fe_add_watch(&mon->chr, G_IO_OUT | G_IO_HUP, |
Marc-André Lureau | 32a6ebe | 2016-10-22 12:52:52 +0300 | [diff] [blame] | 441 | monitor_unblocked, mon); |
Laszlo Ersek | 293d2a0 | 2013-07-16 20:19:41 +0200 | [diff] [blame] | 442 | } |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | |
Paolo Bonzini | 6cff3e8 | 2014-06-18 08:43:59 +0200 | [diff] [blame] | 446 | void monitor_flush(Monitor *mon) |
| 447 | { |
Peter Xu | dc7cbcd | 2018-06-08 11:55:05 +0800 | [diff] [blame] | 448 | qemu_mutex_lock(&mon->mon_lock); |
Paolo Bonzini | 6cff3e8 | 2014-06-18 08:43:59 +0200 | [diff] [blame] | 449 | monitor_flush_locked(mon); |
Peter Xu | dc7cbcd | 2018-06-08 11:55:05 +0800 | [diff] [blame] | 450 | qemu_mutex_unlock(&mon->mon_lock); |
Paolo Bonzini | 6cff3e8 | 2014-06-18 08:43:59 +0200 | [diff] [blame] | 451 | } |
| 452 | |
Luiz Capitulino | e1f2641 | 2013-03-25 13:52:26 -0400 | [diff] [blame] | 453 | /* flush at every end of line */ |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 454 | static void monitor_puts(Monitor *mon, const char *str) |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 455 | { |
ths | 60fe76f | 2007-12-16 03:02:09 +0000 | [diff] [blame] | 456 | char c; |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 457 | |
Peter Xu | dc7cbcd | 2018-06-08 11:55:05 +0800 | [diff] [blame] | 458 | qemu_mutex_lock(&mon->mon_lock); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 459 | for(;;) { |
| 460 | c = *str++; |
| 461 | if (c == '\0') |
| 462 | break; |
Luiz Capitulino | e1f2641 | 2013-03-25 13:52:26 -0400 | [diff] [blame] | 463 | if (c == '\n') { |
| 464 | qstring_append_chr(mon->outbuf, '\r'); |
| 465 | } |
| 466 | qstring_append_chr(mon->outbuf, c); |
| 467 | if (c == '\n') { |
Paolo Bonzini | 6cff3e8 | 2014-06-18 08:43:59 +0200 | [diff] [blame] | 468 | monitor_flush_locked(mon); |
Luiz Capitulino | e1f2641 | 2013-03-25 13:52:26 -0400 | [diff] [blame] | 469 | } |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 470 | } |
Peter Xu | dc7cbcd | 2018-06-08 11:55:05 +0800 | [diff] [blame] | 471 | qemu_mutex_unlock(&mon->mon_lock); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 472 | } |
| 473 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 474 | void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap) |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 475 | { |
Luiz Capitulino | e1f2641 | 2013-03-25 13:52:26 -0400 | [diff] [blame] | 476 | char *buf; |
Luiz Capitulino | b8b0826 | 2010-02-10 23:50:04 -0200 | [diff] [blame] | 477 | |
Luiz Capitulino | 2daa119 | 2009-12-14 18:53:24 -0200 | [diff] [blame] | 478 | if (!mon) |
| 479 | return; |
| 480 | |
Markus Armbruster | 9f3982f | 2015-03-06 19:56:38 +0100 | [diff] [blame] | 481 | if (monitor_is_qmp(mon)) { |
Luiz Capitulino | b8b0826 | 2010-02-10 23:50:04 -0200 | [diff] [blame] | 482 | return; |
Luiz Capitulino | 4a29a85 | 2009-11-26 22:59:05 -0200 | [diff] [blame] | 483 | } |
Luiz Capitulino | b8b0826 | 2010-02-10 23:50:04 -0200 | [diff] [blame] | 484 | |
Luiz Capitulino | e1f2641 | 2013-03-25 13:52:26 -0400 | [diff] [blame] | 485 | buf = g_strdup_vprintf(fmt, ap); |
Luiz Capitulino | b8b0826 | 2010-02-10 23:50:04 -0200 | [diff] [blame] | 486 | monitor_puts(mon, buf); |
Luiz Capitulino | e1f2641 | 2013-03-25 13:52:26 -0400 | [diff] [blame] | 487 | g_free(buf); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 488 | } |
| 489 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 490 | void monitor_printf(Monitor *mon, const char *fmt, ...) |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 491 | { |
| 492 | va_list ap; |
| 493 | va_start(ap, fmt); |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 494 | monitor_vprintf(mon, fmt, ap); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 495 | va_end(ap); |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Pavel Butsykin | caf1531 | 2015-09-22 16:18:17 +0300 | [diff] [blame] | 498 | int monitor_fprintf(FILE *stream, const char *fmt, ...) |
bellard | 7fe4848 | 2004-10-09 18:08:01 +0000 | [diff] [blame] | 499 | { |
| 500 | va_list ap; |
| 501 | va_start(ap, fmt); |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 502 | monitor_vprintf((Monitor *)stream, fmt, ap); |
bellard | 7fe4848 | 2004-10-09 18:08:01 +0000 | [diff] [blame] | 503 | va_end(ap); |
| 504 | return 0; |
| 505 | } |
| 506 | |
Markus Armbruster | 65e3fe6 | 2018-07-03 10:53:50 +0200 | [diff] [blame] | 507 | static void qmp_send_response(Monitor *mon, QDict *rsp) |
Luiz Capitulino | 9b57c02 | 2009-11-26 22:58:58 -0200 | [diff] [blame] | 508 | { |
Markus Armbruster | 65e3fe6 | 2018-07-03 10:53:50 +0200 | [diff] [blame] | 509 | QObject *data = QOBJECT(rsp); |
Luiz Capitulino | 9b57c02 | 2009-11-26 22:58:58 -0200 | [diff] [blame] | 510 | QString *json; |
| 511 | |
Luiz Capitulino | 83a27d4 | 2010-11-22 17:10:37 -0200 | [diff] [blame] | 512 | json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) : |
| 513 | qobject_to_json(data); |
Luiz Capitulino | 9b57c02 | 2009-11-26 22:58:58 -0200 | [diff] [blame] | 514 | assert(json != NULL); |
| 515 | |
Luiz Capitulino | b8b0826 | 2010-02-10 23:50:04 -0200 | [diff] [blame] | 516 | qstring_append_chr(json, '\n'); |
| 517 | monitor_puts(mon, qstring_get_str(json)); |
Luiz Capitulino | 4a29a85 | 2009-11-26 22:59:05 -0200 | [diff] [blame] | 518 | |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 519 | qobject_unref(json); |
Luiz Capitulino | 9b57c02 | 2009-11-26 22:58:58 -0200 | [diff] [blame] | 520 | } |
| 521 | |
Markus Armbruster | 65e3fe6 | 2018-07-03 10:53:50 +0200 | [diff] [blame] | 522 | static void qmp_queue_response(Monitor *mon, QDict *rsp) |
Peter Xu | abe3cd0 | 2018-03-09 17:00:02 +0800 | [diff] [blame] | 523 | { |
Markus Armbruster | f91dc2a | 2018-07-03 10:53:45 +0200 | [diff] [blame] | 524 | if (mon->use_io_thread) { |
Peter Xu | abe3cd0 | 2018-03-09 17:00:02 +0800 | [diff] [blame] | 525 | /* |
Markus Armbruster | 774a6b6 | 2018-07-03 10:53:57 +0200 | [diff] [blame] | 526 | * Push a reference to the response queue. The I/O thread |
| 527 | * drains that queue and emits. |
Peter Xu | abe3cd0 | 2018-03-09 17:00:02 +0800 | [diff] [blame] | 528 | */ |
Peter Xu | abe3cd0 | 2018-03-09 17:00:02 +0800 | [diff] [blame] | 529 | qemu_mutex_lock(&mon->qmp.qmp_queue_lock); |
Markus Armbruster | 65e3fe6 | 2018-07-03 10:53:50 +0200 | [diff] [blame] | 530 | g_queue_push_tail(mon->qmp.qmp_responses, qobject_ref(rsp)); |
Peter Xu | abe3cd0 | 2018-03-09 17:00:02 +0800 | [diff] [blame] | 531 | qemu_mutex_unlock(&mon->qmp.qmp_queue_lock); |
Markus Armbruster | cab5ad8 | 2018-07-03 10:53:46 +0200 | [diff] [blame] | 532 | qemu_bh_schedule(qmp_respond_bh); |
Peter Xu | abe3cd0 | 2018-03-09 17:00:02 +0800 | [diff] [blame] | 533 | } else { |
| 534 | /* |
Markus Armbruster | 774a6b6 | 2018-07-03 10:53:57 +0200 | [diff] [blame] | 535 | * Not using monitor I/O thread, i.e. we are in the main thread. |
| 536 | * Emit right away. |
Peter Xu | abe3cd0 | 2018-03-09 17:00:02 +0800 | [diff] [blame] | 537 | */ |
Markus Armbruster | 65e3fe6 | 2018-07-03 10:53:50 +0200 | [diff] [blame] | 538 | qmp_send_response(mon, rsp); |
Peter Xu | abe3cd0 | 2018-03-09 17:00:02 +0800 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | |
| 542 | struct QMPResponse { |
| 543 | Monitor *mon; |
Markus Armbruster | d43b169 | 2018-07-03 10:53:49 +0200 | [diff] [blame] | 544 | QDict *data; |
Peter Xu | abe3cd0 | 2018-03-09 17:00:02 +0800 | [diff] [blame] | 545 | }; |
| 546 | typedef struct QMPResponse QMPResponse; |
| 547 | |
Markus Armbruster | d43b169 | 2018-07-03 10:53:49 +0200 | [diff] [blame] | 548 | static QDict *monitor_qmp_response_pop_one(Monitor *mon) |
Peter Xu | c73a843 | 2018-06-20 15:32:19 +0800 | [diff] [blame] | 549 | { |
Markus Armbruster | d43b169 | 2018-07-03 10:53:49 +0200 | [diff] [blame] | 550 | QDict *data; |
Peter Xu | c73a843 | 2018-06-20 15:32:19 +0800 | [diff] [blame] | 551 | |
| 552 | qemu_mutex_lock(&mon->qmp.qmp_queue_lock); |
| 553 | data = g_queue_pop_head(mon->qmp.qmp_responses); |
| 554 | qemu_mutex_unlock(&mon->qmp.qmp_queue_lock); |
| 555 | |
| 556 | return data; |
| 557 | } |
| 558 | |
| 559 | static void monitor_qmp_response_flush(Monitor *mon) |
| 560 | { |
Markus Armbruster | d43b169 | 2018-07-03 10:53:49 +0200 | [diff] [blame] | 561 | QDict *data; |
Peter Xu | c73a843 | 2018-06-20 15:32:19 +0800 | [diff] [blame] | 562 | |
| 563 | while ((data = monitor_qmp_response_pop_one(mon))) { |
Markus Armbruster | 65e3fe6 | 2018-07-03 10:53:50 +0200 | [diff] [blame] | 564 | qmp_send_response(mon, data); |
Peter Xu | c73a843 | 2018-06-20 15:32:19 +0800 | [diff] [blame] | 565 | qobject_unref(data); |
| 566 | } |
| 567 | } |
| 568 | |
Peter Xu | abe3cd0 | 2018-03-09 17:00:02 +0800 | [diff] [blame] | 569 | /* |
Peter Xu | 40687eb | 2018-06-20 15:32:18 +0800 | [diff] [blame] | 570 | * Pop a QMPResponse from any monitor's response queue into @response. |
| 571 | * Return false if all the queues are empty; else true. |
Peter Xu | abe3cd0 | 2018-03-09 17:00:02 +0800 | [diff] [blame] | 572 | */ |
Peter Xu | 40687eb | 2018-06-20 15:32:18 +0800 | [diff] [blame] | 573 | static bool monitor_qmp_response_pop_any(QMPResponse *response) |
Peter Xu | abe3cd0 | 2018-03-09 17:00:02 +0800 | [diff] [blame] | 574 | { |
| 575 | Monitor *mon; |
Markus Armbruster | d43b169 | 2018-07-03 10:53:49 +0200 | [diff] [blame] | 576 | QDict *data = NULL; |
Peter Xu | abe3cd0 | 2018-03-09 17:00:02 +0800 | [diff] [blame] | 577 | |
| 578 | qemu_mutex_lock(&monitor_lock); |
| 579 | QTAILQ_FOREACH(mon, &mon_list, entry) { |
Peter Xu | c73a843 | 2018-06-20 15:32:19 +0800 | [diff] [blame] | 580 | data = monitor_qmp_response_pop_one(mon); |
Peter Xu | abe3cd0 | 2018-03-09 17:00:02 +0800 | [diff] [blame] | 581 | if (data) { |
Peter Xu | 40687eb | 2018-06-20 15:32:18 +0800 | [diff] [blame] | 582 | response->mon = mon; |
| 583 | response->data = data; |
Peter Xu | abe3cd0 | 2018-03-09 17:00:02 +0800 | [diff] [blame] | 584 | break; |
| 585 | } |
| 586 | } |
| 587 | qemu_mutex_unlock(&monitor_lock); |
Peter Xu | 40687eb | 2018-06-20 15:32:18 +0800 | [diff] [blame] | 588 | return data != NULL; |
Peter Xu | abe3cd0 | 2018-03-09 17:00:02 +0800 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | static void monitor_qmp_bh_responder(void *opaque) |
| 592 | { |
| 593 | QMPResponse response; |
| 594 | |
Peter Xu | 40687eb | 2018-06-20 15:32:18 +0800 | [diff] [blame] | 595 | while (monitor_qmp_response_pop_any(&response)) { |
Markus Armbruster | 65e3fe6 | 2018-07-03 10:53:50 +0200 | [diff] [blame] | 596 | qmp_send_response(response.mon, response.data); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 597 | qobject_unref(response.data); |
Peter Xu | abe3cd0 | 2018-03-09 17:00:02 +0800 | [diff] [blame] | 598 | } |
| 599 | } |
| 600 | |
Eric Blake | 7fb1cf1 | 2015-11-18 01:52:57 -0700 | [diff] [blame] | 601 | static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = { |
Markus Armbruster | b9b03ab | 2015-10-15 17:08:33 +0200 | [diff] [blame] | 602 | /* Limit guest-triggerable events to 1 per second */ |
| 603 | [QAPI_EVENT_RTC_CHANGE] = { 1000 * SCALE_MS }, |
| 604 | [QAPI_EVENT_WATCHDOG] = { 1000 * SCALE_MS }, |
| 605 | [QAPI_EVENT_BALLOON_CHANGE] = { 1000 * SCALE_MS }, |
| 606 | [QAPI_EVENT_QUORUM_REPORT_BAD] = { 1000 * SCALE_MS }, |
| 607 | [QAPI_EVENT_QUORUM_FAILURE] = { 1000 * SCALE_MS }, |
| 608 | [QAPI_EVENT_VSERPORT_CHANGE] = { 1000 * SCALE_MS }, |
| 609 | }; |
| 610 | |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 611 | /* |
Markus Armbruster | 774a6b6 | 2018-07-03 10:53:57 +0200 | [diff] [blame] | 612 | * Broadcast an event to all monitors. |
| 613 | * @qdict is the event object. Its member "event" must match @event. |
| 614 | * Caller must hold monitor_lock. |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 615 | */ |
Markus Armbruster | 688b4b7 | 2015-10-15 17:08:30 +0200 | [diff] [blame] | 616 | static void monitor_qapi_event_emit(QAPIEvent event, QDict *qdict) |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 617 | { |
| 618 | Monitor *mon; |
| 619 | |
Markus Armbruster | 688b4b7 | 2015-10-15 17:08:30 +0200 | [diff] [blame] | 620 | trace_monitor_protocol_event_emit(event, qdict); |
Peter Xu | 238d9f3 | 2018-03-09 16:59:51 +0800 | [diff] [blame] | 621 | QTAILQ_FOREACH(mon, &mon_list, entry) { |
Markus Armbruster | 635db18 | 2017-03-03 13:32:27 +0100 | [diff] [blame] | 622 | if (monitor_is_qmp(mon) |
| 623 | && mon->qmp.commands != &qmp_cap_negotiation_commands) { |
Markus Armbruster | 65e3fe6 | 2018-07-03 10:53:50 +0200 | [diff] [blame] | 624 | qmp_queue_response(mon, qdict); |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 625 | } |
| 626 | } |
| 627 | } |
| 628 | |
Markus Armbruster | a24712a | 2015-10-15 17:08:34 +0200 | [diff] [blame] | 629 | static void monitor_qapi_event_handler(void *opaque); |
| 630 | |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 631 | /* |
| 632 | * Queue a new event for emission to Monitor instances, |
| 633 | * applying any rate limiting if required. |
| 634 | */ |
| 635 | static void |
Marc-André Lureau | 9a10540 | 2018-07-31 17:01:44 +0200 | [diff] [blame] | 636 | monitor_qapi_event_queue_no_reenter(QAPIEvent event, QDict *qdict) |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 637 | { |
Markus Armbruster | b9b03ab | 2015-10-15 17:08:33 +0200 | [diff] [blame] | 638 | MonitorQAPIEventConf *evconf; |
Wenchao Xia | 43a14cf | 2014-06-18 08:43:31 +0200 | [diff] [blame] | 639 | MonitorQAPIEventState *evstate; |
Markus Armbruster | 93f8f98 | 2015-10-15 17:08:31 +0200 | [diff] [blame] | 640 | |
Eric Blake | 7fb1cf1 | 2015-11-18 01:52:57 -0700 | [diff] [blame] | 641 | assert(event < QAPI_EVENT__MAX); |
Markus Armbruster | b9b03ab | 2015-10-15 17:08:33 +0200 | [diff] [blame] | 642 | evconf = &monitor_qapi_event_conf[event]; |
Markus Armbruster | b9b03ab | 2015-10-15 17:08:33 +0200 | [diff] [blame] | 643 | trace_monitor_protocol_event_queue(event, qdict, evconf->rate); |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 644 | |
Paolo Bonzini | d622cb5 | 2014-06-18 08:44:00 +0200 | [diff] [blame] | 645 | qemu_mutex_lock(&monitor_lock); |
Markus Armbruster | 93f8f98 | 2015-10-15 17:08:31 +0200 | [diff] [blame] | 646 | |
Markus Armbruster | b9b03ab | 2015-10-15 17:08:33 +0200 | [diff] [blame] | 647 | if (!evconf->rate) { |
Markus Armbruster | 93f8f98 | 2015-10-15 17:08:31 +0200 | [diff] [blame] | 648 | /* Unthrottled event */ |
Markus Armbruster | 688b4b7 | 2015-10-15 17:08:30 +0200 | [diff] [blame] | 649 | monitor_qapi_event_emit(event, qdict); |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 650 | } else { |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 651 | QDict *data = qobject_to(QDict, qdict_get(qdict, "data")); |
Markus Armbruster | 7de0be6 | 2015-10-15 17:08:35 +0200 | [diff] [blame] | 652 | MonitorQAPIEventState key = { .event = event, .data = data }; |
Markus Armbruster | a24712a | 2015-10-15 17:08:34 +0200 | [diff] [blame] | 653 | |
| 654 | evstate = g_hash_table_lookup(monitor_qapi_event_state, &key); |
| 655 | assert(!evstate || timer_pending(evstate->timer)); |
| 656 | |
| 657 | if (evstate) { |
Markus Armbruster | 93f8f98 | 2015-10-15 17:08:31 +0200 | [diff] [blame] | 658 | /* |
Markus Armbruster | b9b03ab | 2015-10-15 17:08:33 +0200 | [diff] [blame] | 659 | * Timer is pending for (at least) evconf->rate ns after |
Markus Armbruster | 93f8f98 | 2015-10-15 17:08:31 +0200 | [diff] [blame] | 660 | * last send. Store event for sending when timer fires, |
| 661 | * replacing a prior stored event if any. |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 662 | */ |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 663 | qobject_unref(evstate->qdict); |
Marc-André Lureau | f5a74a5 | 2018-04-19 17:01:44 +0200 | [diff] [blame] | 664 | evstate->qdict = qobject_ref(qdict); |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 665 | } else { |
Markus Armbruster | 93f8f98 | 2015-10-15 17:08:31 +0200 | [diff] [blame] | 666 | /* |
Markus Armbruster | b9b03ab | 2015-10-15 17:08:33 +0200 | [diff] [blame] | 667 | * Last send was (at least) evconf->rate ns ago. |
Markus Armbruster | 93f8f98 | 2015-10-15 17:08:31 +0200 | [diff] [blame] | 668 | * Send immediately, and arm the timer to call |
Markus Armbruster | b9b03ab | 2015-10-15 17:08:33 +0200 | [diff] [blame] | 669 | * monitor_qapi_event_handler() in evconf->rate ns. Any |
Markus Armbruster | 93f8f98 | 2015-10-15 17:08:31 +0200 | [diff] [blame] | 670 | * events arriving before then will be delayed until then. |
| 671 | */ |
Peter Xu | 6e8c5f4 | 2018-06-08 11:55:09 +0800 | [diff] [blame] | 672 | int64_t now = qemu_clock_get_ns(monitor_get_event_clock()); |
Markus Armbruster | 93f8f98 | 2015-10-15 17:08:31 +0200 | [diff] [blame] | 673 | |
Markus Armbruster | 688b4b7 | 2015-10-15 17:08:30 +0200 | [diff] [blame] | 674 | monitor_qapi_event_emit(event, qdict); |
Markus Armbruster | a24712a | 2015-10-15 17:08:34 +0200 | [diff] [blame] | 675 | |
| 676 | evstate = g_new(MonitorQAPIEventState, 1); |
| 677 | evstate->event = event; |
Marc-André Lureau | f5a74a5 | 2018-04-19 17:01:44 +0200 | [diff] [blame] | 678 | evstate->data = qobject_ref(data); |
Markus Armbruster | a24712a | 2015-10-15 17:08:34 +0200 | [diff] [blame] | 679 | evstate->qdict = NULL; |
Peter Xu | 6e8c5f4 | 2018-06-08 11:55:09 +0800 | [diff] [blame] | 680 | evstate->timer = timer_new_ns(monitor_get_event_clock(), |
Markus Armbruster | a24712a | 2015-10-15 17:08:34 +0200 | [diff] [blame] | 681 | monitor_qapi_event_handler, |
| 682 | evstate); |
| 683 | g_hash_table_add(monitor_qapi_event_state, evstate); |
Markus Armbruster | b9b03ab | 2015-10-15 17:08:33 +0200 | [diff] [blame] | 684 | timer_mod_ns(evstate->timer, now + evconf->rate); |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 685 | } |
| 686 | } |
Markus Armbruster | 93f8f98 | 2015-10-15 17:08:31 +0200 | [diff] [blame] | 687 | |
Paolo Bonzini | d622cb5 | 2014-06-18 08:44:00 +0200 | [diff] [blame] | 688 | qemu_mutex_unlock(&monitor_lock); |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 689 | } |
| 690 | |
Marc-André Lureau | 9a10540 | 2018-07-31 17:01:44 +0200 | [diff] [blame] | 691 | static void |
| 692 | monitor_qapi_event_queue(QAPIEvent event, QDict *qdict, Error **errp) |
| 693 | { |
| 694 | /* |
| 695 | * monitor_qapi_event_queue_no_reenter() is not reentrant: it |
| 696 | * would deadlock on monitor_lock. Work around by queueing |
| 697 | * events in thread-local storage. |
| 698 | * TODO: remove this, make it re-enter safe. |
| 699 | */ |
| 700 | typedef struct MonitorQapiEvent { |
| 701 | QAPIEvent event; |
| 702 | QDict *qdict; |
| 703 | QSIMPLEQ_ENTRY(MonitorQapiEvent) entry; |
| 704 | } MonitorQapiEvent; |
| 705 | static __thread QSIMPLEQ_HEAD(, MonitorQapiEvent) event_queue; |
| 706 | static __thread bool reentered; |
| 707 | MonitorQapiEvent *ev; |
| 708 | |
| 709 | if (!reentered) { |
| 710 | QSIMPLEQ_INIT(&event_queue); |
| 711 | } |
| 712 | |
| 713 | ev = g_new(MonitorQapiEvent, 1); |
| 714 | ev->qdict = qobject_ref(qdict); |
| 715 | ev->event = event; |
| 716 | QSIMPLEQ_INSERT_TAIL(&event_queue, ev, entry); |
| 717 | if (reentered) { |
| 718 | return; |
| 719 | } |
| 720 | |
| 721 | reentered = true; |
| 722 | |
| 723 | while ((ev = QSIMPLEQ_FIRST(&event_queue)) != NULL) { |
| 724 | QSIMPLEQ_REMOVE_HEAD(&event_queue, entry); |
| 725 | monitor_qapi_event_queue_no_reenter(ev->event, ev->qdict); |
| 726 | qobject_unref(ev->qdict); |
| 727 | g_free(ev); |
| 728 | } |
| 729 | |
| 730 | reentered = false; |
| 731 | } |
| 732 | |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 733 | /* |
Markus Armbruster | b9b03ab | 2015-10-15 17:08:33 +0200 | [diff] [blame] | 734 | * This function runs evconf->rate ns after sending a throttled |
Markus Armbruster | 93f8f98 | 2015-10-15 17:08:31 +0200 | [diff] [blame] | 735 | * event. |
| 736 | * If another event has since been stored, send it. |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 737 | */ |
Wenchao Xia | 43a14cf | 2014-06-18 08:43:31 +0200 | [diff] [blame] | 738 | static void monitor_qapi_event_handler(void *opaque) |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 739 | { |
Wenchao Xia | 43a14cf | 2014-06-18 08:43:31 +0200 | [diff] [blame] | 740 | MonitorQAPIEventState *evstate = opaque; |
Markus Armbruster | b9b03ab | 2015-10-15 17:08:33 +0200 | [diff] [blame] | 741 | MonitorQAPIEventConf *evconf = &monitor_qapi_event_conf[evstate->event]; |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 742 | |
Markus Armbruster | 93f8f98 | 2015-10-15 17:08:31 +0200 | [diff] [blame] | 743 | trace_monitor_protocol_event_handler(evstate->event, evstate->qdict); |
Paolo Bonzini | d622cb5 | 2014-06-18 08:44:00 +0200 | [diff] [blame] | 744 | qemu_mutex_lock(&monitor_lock); |
Markus Armbruster | 93f8f98 | 2015-10-15 17:08:31 +0200 | [diff] [blame] | 745 | |
Markus Armbruster | 688b4b7 | 2015-10-15 17:08:30 +0200 | [diff] [blame] | 746 | if (evstate->qdict) { |
Peter Xu | 6e8c5f4 | 2018-06-08 11:55:09 +0800 | [diff] [blame] | 747 | int64_t now = qemu_clock_get_ns(monitor_get_event_clock()); |
Markus Armbruster | 93f8f98 | 2015-10-15 17:08:31 +0200 | [diff] [blame] | 748 | |
Markus Armbruster | 688b4b7 | 2015-10-15 17:08:30 +0200 | [diff] [blame] | 749 | monitor_qapi_event_emit(evstate->event, evstate->qdict); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 750 | qobject_unref(evstate->qdict); |
Markus Armbruster | 688b4b7 | 2015-10-15 17:08:30 +0200 | [diff] [blame] | 751 | evstate->qdict = NULL; |
Markus Armbruster | b9b03ab | 2015-10-15 17:08:33 +0200 | [diff] [blame] | 752 | timer_mod_ns(evstate->timer, now + evconf->rate); |
Markus Armbruster | a24712a | 2015-10-15 17:08:34 +0200 | [diff] [blame] | 753 | } else { |
| 754 | g_hash_table_remove(monitor_qapi_event_state, evstate); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 755 | qobject_unref(evstate->data); |
Markus Armbruster | a24712a | 2015-10-15 17:08:34 +0200 | [diff] [blame] | 756 | timer_free(evstate->timer); |
| 757 | g_free(evstate); |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 758 | } |
Markus Armbruster | 93f8f98 | 2015-10-15 17:08:31 +0200 | [diff] [blame] | 759 | |
Paolo Bonzini | d622cb5 | 2014-06-18 08:44:00 +0200 | [diff] [blame] | 760 | qemu_mutex_unlock(&monitor_lock); |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 761 | } |
| 762 | |
Markus Armbruster | a24712a | 2015-10-15 17:08:34 +0200 | [diff] [blame] | 763 | static unsigned int qapi_event_throttle_hash(const void *key) |
| 764 | { |
| 765 | const MonitorQAPIEventState *evstate = key; |
Markus Armbruster | 7de0be6 | 2015-10-15 17:08:35 +0200 | [diff] [blame] | 766 | unsigned int hash = evstate->event * 255; |
Markus Armbruster | a24712a | 2015-10-15 17:08:34 +0200 | [diff] [blame] | 767 | |
Markus Armbruster | 7de0be6 | 2015-10-15 17:08:35 +0200 | [diff] [blame] | 768 | if (evstate->event == QAPI_EVENT_VSERPORT_CHANGE) { |
| 769 | hash += g_str_hash(qdict_get_str(evstate->data, "id")); |
| 770 | } |
| 771 | |
Alberto Garcia | 6d425eb | 2016-03-10 13:55:25 +0200 | [diff] [blame] | 772 | if (evstate->event == QAPI_EVENT_QUORUM_REPORT_BAD) { |
| 773 | hash += g_str_hash(qdict_get_str(evstate->data, "node-name")); |
| 774 | } |
| 775 | |
Markus Armbruster | 7de0be6 | 2015-10-15 17:08:35 +0200 | [diff] [blame] | 776 | return hash; |
Markus Armbruster | a24712a | 2015-10-15 17:08:34 +0200 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | static gboolean qapi_event_throttle_equal(const void *a, const void *b) |
| 780 | { |
| 781 | const MonitorQAPIEventState *eva = a; |
| 782 | const MonitorQAPIEventState *evb = b; |
| 783 | |
Markus Armbruster | 7de0be6 | 2015-10-15 17:08:35 +0200 | [diff] [blame] | 784 | if (eva->event != evb->event) { |
| 785 | return FALSE; |
| 786 | } |
| 787 | |
| 788 | if (eva->event == QAPI_EVENT_VSERPORT_CHANGE) { |
| 789 | return !strcmp(qdict_get_str(eva->data, "id"), |
| 790 | qdict_get_str(evb->data, "id")); |
| 791 | } |
| 792 | |
Alberto Garcia | 6d425eb | 2016-03-10 13:55:25 +0200 | [diff] [blame] | 793 | if (eva->event == QAPI_EVENT_QUORUM_REPORT_BAD) { |
| 794 | return !strcmp(qdict_get_str(eva->data, "node-name"), |
| 795 | qdict_get_str(evb->data, "node-name")); |
| 796 | } |
| 797 | |
Markus Armbruster | 7de0be6 | 2015-10-15 17:08:35 +0200 | [diff] [blame] | 798 | return TRUE; |
Markus Armbruster | a24712a | 2015-10-15 17:08:34 +0200 | [diff] [blame] | 799 | } |
| 800 | |
Wenchao Xia | 43a14cf | 2014-06-18 08:43:31 +0200 | [diff] [blame] | 801 | static void monitor_qapi_event_init(void) |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 802 | { |
Markus Armbruster | a24712a | 2015-10-15 17:08:34 +0200 | [diff] [blame] | 803 | monitor_qapi_event_state = g_hash_table_new(qapi_event_throttle_hash, |
| 804 | qapi_event_throttle_equal); |
Wenchao Xia | 43a14cf | 2014-06-18 08:43:31 +0200 | [diff] [blame] | 805 | qmp_event_set_func_emit(monitor_qapi_event_queue); |
Luiz Capitulino | 0d1ea87 | 2009-11-26 22:59:03 -0200 | [diff] [blame] | 806 | } |
| 807 | |
Markus Armbruster | 7ef6cf6 | 2015-03-06 19:12:36 +0100 | [diff] [blame] | 808 | static void handle_hmp_command(Monitor *mon, const char *cmdline); |
Luiz Capitulino | 0268d97 | 2010-10-22 10:08:02 -0200 | [diff] [blame] | 809 | |
Peter Xu | a5ed352 | 2018-03-09 16:59:52 +0800 | [diff] [blame] | 810 | static void monitor_data_init(Monitor *mon, bool skip_flush, |
Markus Armbruster | f91dc2a | 2018-07-03 10:53:45 +0200 | [diff] [blame] | 811 | bool use_io_thread) |
Wenchao Xia | b01fe89 | 2013-08-27 20:38:19 +0800 | [diff] [blame] | 812 | { |
| 813 | memset(mon, 0, sizeof(Monitor)); |
Peter Xu | dc7cbcd | 2018-06-08 11:55:05 +0800 | [diff] [blame] | 814 | qemu_mutex_init(&mon->mon_lock); |
Peter Xu | 71da466 | 2018-03-09 16:59:57 +0800 | [diff] [blame] | 815 | qemu_mutex_init(&mon->qmp.qmp_queue_lock); |
Wenchao Xia | b01fe89 | 2013-08-27 20:38:19 +0800 | [diff] [blame] | 816 | mon->outbuf = qstring_new(); |
Wenchao Xia | 7717239 | 2013-08-27 20:38:20 +0800 | [diff] [blame] | 817 | /* Use *mon_cmds by default. */ |
| 818 | mon->cmd_table = mon_cmds; |
Peter Xu | 88a95d1 | 2018-03-09 16:59:48 +0800 | [diff] [blame] | 819 | mon->skip_flush = skip_flush; |
Markus Armbruster | f91dc2a | 2018-07-03 10:53:45 +0200 | [diff] [blame] | 820 | mon->use_io_thread = use_io_thread; |
Peter Xu | 71da466 | 2018-03-09 16:59:57 +0800 | [diff] [blame] | 821 | mon->qmp.qmp_requests = g_queue_new(); |
Peter Xu | abe3cd0 | 2018-03-09 17:00:02 +0800 | [diff] [blame] | 822 | mon->qmp.qmp_responses = g_queue_new(); |
Wenchao Xia | b01fe89 | 2013-08-27 20:38:19 +0800 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | static void monitor_data_destroy(Monitor *mon) |
| 826 | { |
Greg Kurz | 751f8cf | 2017-10-17 10:16:22 +0200 | [diff] [blame] | 827 | g_free(mon->mon_cpu_path); |
Marc-André Lureau | 1ce2610 | 2017-01-27 00:49:13 +0400 | [diff] [blame] | 828 | qemu_chr_fe_deinit(&mon->chr, false); |
Marc-André Lureau | 2ef4571 | 2016-08-01 15:23:42 +0400 | [diff] [blame] | 829 | if (monitor_is_qmp(mon)) { |
| 830 | json_message_parser_destroy(&mon->qmp.parser); |
| 831 | } |
Marc-André Lureau | e5dc1a6 | 2018-01-04 17:05:15 +0100 | [diff] [blame] | 832 | readline_free(mon->rs); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 833 | qobject_unref(mon->outbuf); |
Peter Xu | dc7cbcd | 2018-06-08 11:55:05 +0800 | [diff] [blame] | 834 | qemu_mutex_destroy(&mon->mon_lock); |
Peter Xu | 71da466 | 2018-03-09 16:59:57 +0800 | [diff] [blame] | 835 | qemu_mutex_destroy(&mon->qmp.qmp_queue_lock); |
Peter Xu | 6d2d563 | 2018-03-26 14:38:55 +0800 | [diff] [blame] | 836 | monitor_qmp_cleanup_req_queue_locked(mon); |
| 837 | monitor_qmp_cleanup_resp_queue_locked(mon); |
Peter Xu | 71da466 | 2018-03-09 16:59:57 +0800 | [diff] [blame] | 838 | g_queue_free(mon->qmp.qmp_requests); |
Peter Xu | abe3cd0 | 2018-03-09 17:00:02 +0800 | [diff] [blame] | 839 | g_queue_free(mon->qmp.qmp_responses); |
Wenchao Xia | b01fe89 | 2013-08-27 20:38:19 +0800 | [diff] [blame] | 840 | } |
| 841 | |
Luiz Capitulino | d51a67b | 2011-11-25 17:52:45 -0200 | [diff] [blame] | 842 | char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index, |
| 843 | int64_t cpu_index, Error **errp) |
Luiz Capitulino | 0268d97 | 2010-10-22 10:08:02 -0200 | [diff] [blame] | 844 | { |
Luiz Capitulino | d51a67b | 2011-11-25 17:52:45 -0200 | [diff] [blame] | 845 | char *output = NULL; |
Luiz Capitulino | 0268d97 | 2010-10-22 10:08:02 -0200 | [diff] [blame] | 846 | Monitor *old_mon, hmp; |
Luiz Capitulino | 0268d97 | 2010-10-22 10:08:02 -0200 | [diff] [blame] | 847 | |
Peter Xu | a5ed352 | 2018-03-09 16:59:52 +0800 | [diff] [blame] | 848 | monitor_data_init(&hmp, true, false); |
Luiz Capitulino | 0268d97 | 2010-10-22 10:08:02 -0200 | [diff] [blame] | 849 | |
| 850 | old_mon = cur_mon; |
| 851 | cur_mon = &hmp; |
| 852 | |
Luiz Capitulino | d51a67b | 2011-11-25 17:52:45 -0200 | [diff] [blame] | 853 | if (has_cpu_index) { |
| 854 | int ret = monitor_set_cpu(cpu_index); |
Luiz Capitulino | 0268d97 | 2010-10-22 10:08:02 -0200 | [diff] [blame] | 855 | if (ret < 0) { |
| 856 | cur_mon = old_mon; |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 857 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index", |
| 858 | "a CPU number"); |
Luiz Capitulino | 0268d97 | 2010-10-22 10:08:02 -0200 | [diff] [blame] | 859 | goto out; |
| 860 | } |
| 861 | } |
| 862 | |
Markus Armbruster | 7ef6cf6 | 2015-03-06 19:12:36 +0100 | [diff] [blame] | 863 | handle_hmp_command(&hmp, command_line); |
Luiz Capitulino | 0268d97 | 2010-10-22 10:08:02 -0200 | [diff] [blame] | 864 | cur_mon = old_mon; |
| 865 | |
Peter Xu | dc7cbcd | 2018-06-08 11:55:05 +0800 | [diff] [blame] | 866 | qemu_mutex_lock(&hmp.mon_lock); |
Luiz Capitulino | 48c043d | 2013-04-02 15:07:33 -0400 | [diff] [blame] | 867 | if (qstring_get_length(hmp.outbuf) > 0) { |
| 868 | output = g_strdup(qstring_get_str(hmp.outbuf)); |
Luiz Capitulino | d51a67b | 2011-11-25 17:52:45 -0200 | [diff] [blame] | 869 | } else { |
| 870 | output = g_strdup(""); |
Luiz Capitulino | 0268d97 | 2010-10-22 10:08:02 -0200 | [diff] [blame] | 871 | } |
Peter Xu | dc7cbcd | 2018-06-08 11:55:05 +0800 | [diff] [blame] | 872 | qemu_mutex_unlock(&hmp.mon_lock); |
Luiz Capitulino | 0268d97 | 2010-10-22 10:08:02 -0200 | [diff] [blame] | 873 | |
| 874 | out: |
Wenchao Xia | b01fe89 | 2013-08-27 20:38:19 +0800 | [diff] [blame] | 875 | monitor_data_destroy(&hmp); |
Luiz Capitulino | d51a67b | 2011-11-25 17:52:45 -0200 | [diff] [blame] | 876 | return output; |
Luiz Capitulino | 0268d97 | 2010-10-22 10:08:02 -0200 | [diff] [blame] | 877 | } |
| 878 | |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 879 | static int compare_cmd(const char *name, const char *list) |
| 880 | { |
| 881 | const char *p, *pstart; |
| 882 | int len; |
| 883 | len = strlen(name); |
| 884 | p = list; |
| 885 | for(;;) { |
| 886 | pstart = p; |
Keno Fischer | 5c99fa3 | 2018-06-29 12:32:10 +0200 | [diff] [blame] | 887 | p = qemu_strchrnul(p, '|'); |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 888 | if ((p - pstart) == len && !memcmp(pstart, name, len)) |
| 889 | return 1; |
| 890 | if (*p == '\0') |
| 891 | break; |
| 892 | p++; |
| 893 | } |
| 894 | return 0; |
| 895 | } |
| 896 | |
Wenchao Xia | f5438c0 | 2013-08-27 20:38:21 +0800 | [diff] [blame] | 897 | static int get_str(char *buf, int buf_size, const char **pp) |
| 898 | { |
| 899 | const char *p; |
| 900 | char *q; |
| 901 | int c; |
| 902 | |
| 903 | q = buf; |
| 904 | p = *pp; |
| 905 | while (qemu_isspace(*p)) { |
| 906 | p++; |
| 907 | } |
| 908 | if (*p == '\0') { |
| 909 | fail: |
| 910 | *q = '\0'; |
| 911 | *pp = p; |
| 912 | return -1; |
| 913 | } |
| 914 | if (*p == '\"') { |
| 915 | p++; |
| 916 | while (*p != '\0' && *p != '\"') { |
| 917 | if (*p == '\\') { |
| 918 | p++; |
| 919 | c = *p++; |
| 920 | switch (c) { |
| 921 | case 'n': |
| 922 | c = '\n'; |
| 923 | break; |
| 924 | case 'r': |
| 925 | c = '\r'; |
| 926 | break; |
| 927 | case '\\': |
| 928 | case '\'': |
| 929 | case '\"': |
| 930 | break; |
| 931 | default: |
Peter Maydell | 71baf78 | 2015-08-19 16:20:19 +0100 | [diff] [blame] | 932 | printf("unsupported escape code: '\\%c'\n", c); |
Wenchao Xia | f5438c0 | 2013-08-27 20:38:21 +0800 | [diff] [blame] | 933 | goto fail; |
| 934 | } |
| 935 | if ((q - buf) < buf_size - 1) { |
| 936 | *q++ = c; |
| 937 | } |
| 938 | } else { |
| 939 | if ((q - buf) < buf_size - 1) { |
| 940 | *q++ = *p; |
| 941 | } |
| 942 | p++; |
| 943 | } |
| 944 | } |
| 945 | if (*p != '\"') { |
Peter Maydell | 71baf78 | 2015-08-19 16:20:19 +0100 | [diff] [blame] | 946 | printf("unterminated string\n"); |
Wenchao Xia | f5438c0 | 2013-08-27 20:38:21 +0800 | [diff] [blame] | 947 | goto fail; |
| 948 | } |
| 949 | p++; |
| 950 | } else { |
| 951 | while (*p != '\0' && !qemu_isspace(*p)) { |
| 952 | if ((q - buf) < buf_size - 1) { |
| 953 | *q++ = *p; |
| 954 | } |
| 955 | p++; |
| 956 | } |
| 957 | } |
| 958 | *q = '\0'; |
| 959 | *pp = p; |
| 960 | return 0; |
| 961 | } |
| 962 | |
| 963 | #define MAX_ARGS 16 |
| 964 | |
Wenchao Xia | dcc70cd | 2013-08-27 20:38:22 +0800 | [diff] [blame] | 965 | static void free_cmdline_args(char **args, int nb_args) |
| 966 | { |
| 967 | int i; |
| 968 | |
| 969 | assert(nb_args <= MAX_ARGS); |
| 970 | |
| 971 | for (i = 0; i < nb_args; i++) { |
| 972 | g_free(args[i]); |
| 973 | } |
| 974 | |
| 975 | } |
| 976 | |
| 977 | /* |
| 978 | * Parse the command line to get valid args. |
| 979 | * @cmdline: command line to be parsed. |
| 980 | * @pnb_args: location to store the number of args, must NOT be NULL. |
| 981 | * @args: location to store the args, which should be freed by caller, must |
| 982 | * NOT be NULL. |
| 983 | * |
| 984 | * Returns 0 on success, negative on failure. |
| 985 | * |
| 986 | * NOTE: this parser is an approximate form of the real command parser. Number |
| 987 | * of args have a limit of MAX_ARGS. If cmdline contains more, it will |
| 988 | * return with failure. |
| 989 | */ |
| 990 | static int parse_cmdline(const char *cmdline, |
| 991 | int *pnb_args, char **args) |
Wenchao Xia | f5438c0 | 2013-08-27 20:38:21 +0800 | [diff] [blame] | 992 | { |
| 993 | const char *p; |
| 994 | int nb_args, ret; |
| 995 | char buf[1024]; |
| 996 | |
| 997 | p = cmdline; |
| 998 | nb_args = 0; |
| 999 | for (;;) { |
| 1000 | while (qemu_isspace(*p)) { |
| 1001 | p++; |
| 1002 | } |
| 1003 | if (*p == '\0') { |
| 1004 | break; |
| 1005 | } |
| 1006 | if (nb_args >= MAX_ARGS) { |
Wenchao Xia | dcc70cd | 2013-08-27 20:38:22 +0800 | [diff] [blame] | 1007 | goto fail; |
Wenchao Xia | f5438c0 | 2013-08-27 20:38:21 +0800 | [diff] [blame] | 1008 | } |
| 1009 | ret = get_str(buf, sizeof(buf), &p); |
Wenchao Xia | dcc70cd | 2013-08-27 20:38:22 +0800 | [diff] [blame] | 1010 | if (ret < 0) { |
| 1011 | goto fail; |
| 1012 | } |
Wenchao Xia | f5438c0 | 2013-08-27 20:38:21 +0800 | [diff] [blame] | 1013 | args[nb_args] = g_strdup(buf); |
| 1014 | nb_args++; |
Wenchao Xia | f5438c0 | 2013-08-27 20:38:21 +0800 | [diff] [blame] | 1015 | } |
| 1016 | *pnb_args = nb_args; |
Wenchao Xia | dcc70cd | 2013-08-27 20:38:22 +0800 | [diff] [blame] | 1017 | return 0; |
| 1018 | |
| 1019 | fail: |
| 1020 | free_cmdline_args(args, nb_args); |
| 1021 | return -1; |
Wenchao Xia | f5438c0 | 2013-08-27 20:38:21 +0800 | [diff] [blame] | 1022 | } |
| 1023 | |
Dr. David Alan Gilbert | c3120f7 | 2018-06-20 16:39:41 +0100 | [diff] [blame] | 1024 | /* |
Markus Armbruster | 774a6b6 | 2018-07-03 10:53:57 +0200 | [diff] [blame] | 1025 | * Can command @cmd be executed in preconfig state? |
Dr. David Alan Gilbert | c3120f7 | 2018-06-20 16:39:41 +0100 | [diff] [blame] | 1026 | */ |
| 1027 | static bool cmd_can_preconfig(const mon_cmd_t *cmd) |
| 1028 | { |
| 1029 | if (!cmd->flags) { |
| 1030 | return false; |
| 1031 | } |
| 1032 | |
| 1033 | return strchr(cmd->flags, 'p'); |
| 1034 | } |
| 1035 | |
Wenchao Xia | 6685549 | 2013-08-27 20:38:23 +0800 | [diff] [blame] | 1036 | static void help_cmd_dump_one(Monitor *mon, |
| 1037 | const mon_cmd_t *cmd, |
| 1038 | char **prefix_args, |
| 1039 | int prefix_args_nb) |
| 1040 | { |
| 1041 | int i; |
| 1042 | |
Dr. David Alan Gilbert | 31785f1 | 2018-06-20 16:39:42 +0100 | [diff] [blame] | 1043 | if (runstate_check(RUN_STATE_PRECONFIG) && !cmd_can_preconfig(cmd)) { |
| 1044 | return; |
| 1045 | } |
| 1046 | |
Wenchao Xia | 6685549 | 2013-08-27 20:38:23 +0800 | [diff] [blame] | 1047 | for (i = 0; i < prefix_args_nb; i++) { |
| 1048 | monitor_printf(mon, "%s ", prefix_args[i]); |
| 1049 | } |
| 1050 | monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help); |
| 1051 | } |
| 1052 | |
| 1053 | /* @args[@arg_index] is the valid command need to find in @cmds */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 1054 | static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds, |
Wenchao Xia | 6685549 | 2013-08-27 20:38:23 +0800 | [diff] [blame] | 1055 | char **args, int nb_args, int arg_index) |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 1056 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 1057 | const mon_cmd_t *cmd; |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 1058 | |
Wenchao Xia | 6685549 | 2013-08-27 20:38:23 +0800 | [diff] [blame] | 1059 | /* No valid arg need to compare with, dump all in *cmds */ |
| 1060 | if (arg_index >= nb_args) { |
| 1061 | for (cmd = cmds; cmd->name != NULL; cmd++) { |
| 1062 | help_cmd_dump_one(mon, cmd, args, arg_index); |
| 1063 | } |
| 1064 | return; |
| 1065 | } |
| 1066 | |
| 1067 | /* Find one entry to dump */ |
| 1068 | for (cmd = cmds; cmd->name != NULL; cmd++) { |
Dr. David Alan Gilbert | 31785f1 | 2018-06-20 16:39:42 +0100 | [diff] [blame] | 1069 | if (compare_cmd(args[arg_index], cmd->name) && |
| 1070 | ((!runstate_check(RUN_STATE_PRECONFIG) || |
| 1071 | cmd_can_preconfig(cmd)))) { |
Wenchao Xia | 6685549 | 2013-08-27 20:38:23 +0800 | [diff] [blame] | 1072 | if (cmd->sub_table) { |
| 1073 | /* continue with next arg */ |
| 1074 | help_cmd_dump(mon, cmd->sub_table, |
| 1075 | args, nb_args, arg_index + 1); |
| 1076 | } else { |
| 1077 | help_cmd_dump_one(mon, cmd, args, arg_index); |
| 1078 | } |
| 1079 | break; |
| 1080 | } |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 1081 | } |
| 1082 | } |
| 1083 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1084 | static void help_cmd(Monitor *mon, const char *name) |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 1085 | { |
Wenchao Xia | 6685549 | 2013-08-27 20:38:23 +0800 | [diff] [blame] | 1086 | char *args[MAX_ARGS]; |
| 1087 | int nb_args = 0; |
| 1088 | |
| 1089 | /* 1. parse user input */ |
| 1090 | if (name) { |
| 1091 | /* special case for log, directly dump and return */ |
| 1092 | if (!strcmp(name, "log")) { |
Peter Maydell | 38dad9e | 2013-02-11 16:41:25 +0000 | [diff] [blame] | 1093 | const QEMULogItem *item; |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1094 | monitor_printf(mon, "Log items (comma separated):\n"); |
| 1095 | monitor_printf(mon, "%-10s %s\n", "none", "remove all logs"); |
Peter Maydell | 38dad9e | 2013-02-11 16:41:25 +0000 | [diff] [blame] | 1096 | for (item = qemu_log_items; item->mask != 0; item++) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1097 | monitor_printf(mon, "%-10s %s\n", item->name, item->help); |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1098 | } |
Wenchao Xia | 6685549 | 2013-08-27 20:38:23 +0800 | [diff] [blame] | 1099 | return; |
| 1100 | } |
| 1101 | |
| 1102 | if (parse_cmdline(name, &nb_args, args) < 0) { |
| 1103 | return; |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1104 | } |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 1105 | } |
Wenchao Xia | 6685549 | 2013-08-27 20:38:23 +0800 | [diff] [blame] | 1106 | |
| 1107 | /* 2. dump the contents according to parsed args */ |
| 1108 | help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0); |
| 1109 | |
| 1110 | free_cmdline_args(args, nb_args); |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 1113 | static void do_help_cmd(Monitor *mon, const QDict *qdict) |
Luiz Capitulino | 3818318 | 2009-08-28 15:27:08 -0300 | [diff] [blame] | 1114 | { |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 1115 | help_cmd(mon, qdict_get_try_str(qdict, "name")); |
Luiz Capitulino | 3818318 | 2009-08-28 15:27:08 -0300 | [diff] [blame] | 1116 | } |
| 1117 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 1118 | static void hmp_trace_event(Monitor *mon, const QDict *qdict) |
Prerna Saxena | 22890ab | 2010-06-24 17:04:53 +0530 | [diff] [blame] | 1119 | { |
| 1120 | const char *tp_name = qdict_get_str(qdict, "name"); |
| 1121 | bool new_state = qdict_get_bool(qdict, "option"); |
Lluís Vilanova | 77e2b17 | 2016-07-11 12:53:57 +0200 | [diff] [blame] | 1122 | bool has_vcpu = qdict_haskey(qdict, "vcpu"); |
| 1123 | int vcpu = qdict_get_try_int(qdict, "vcpu", 0); |
Lluís Vilanova | 14101d0 | 2014-08-25 13:20:03 +0200 | [diff] [blame] | 1124 | Error *local_err = NULL; |
Blue Swirl | f871d68 | 2010-10-13 19:14:29 +0000 | [diff] [blame] | 1125 | |
Lluís Vilanova | 77e2b17 | 2016-07-11 12:53:57 +0200 | [diff] [blame] | 1126 | if (vcpu < 0) { |
| 1127 | monitor_printf(mon, "argument vcpu must be positive"); |
| 1128 | return; |
| 1129 | } |
| 1130 | |
| 1131 | qmp_trace_event_set_state(tp_name, new_state, true, true, has_vcpu, vcpu, &local_err); |
Lluís Vilanova | 14101d0 | 2014-08-25 13:20:03 +0200 | [diff] [blame] | 1132 | if (local_err) { |
Markus Armbruster | 091e38b | 2015-02-10 15:15:43 +0100 | [diff] [blame] | 1133 | error_report_err(local_err); |
Blue Swirl | f871d68 | 2010-10-13 19:14:29 +0000 | [diff] [blame] | 1134 | } |
Prerna Saxena | 22890ab | 2010-06-24 17:04:53 +0530 | [diff] [blame] | 1135 | } |
Stefan Hajnoczi | c5ceb52 | 2010-07-13 09:26:33 +0100 | [diff] [blame] | 1136 | |
Michael Roth | c45a816 | 2011-10-02 08:44:37 -0500 | [diff] [blame] | 1137 | #ifdef CONFIG_TRACE_SIMPLE |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 1138 | static void hmp_trace_file(Monitor *mon, const QDict *qdict) |
Stefan Hajnoczi | c5ceb52 | 2010-07-13 09:26:33 +0100 | [diff] [blame] | 1139 | { |
| 1140 | const char *op = qdict_get_try_str(qdict, "op"); |
| 1141 | const char *arg = qdict_get_try_str(qdict, "arg"); |
| 1142 | |
| 1143 | if (!op) { |
| 1144 | st_print_trace_file_status((FILE *)mon, &monitor_fprintf); |
| 1145 | } else if (!strcmp(op, "on")) { |
| 1146 | st_set_trace_file_enabled(true); |
| 1147 | } else if (!strcmp(op, "off")) { |
| 1148 | st_set_trace_file_enabled(false); |
| 1149 | } else if (!strcmp(op, "flush")) { |
| 1150 | st_flush_trace_buffer(); |
| 1151 | } else if (!strcmp(op, "set")) { |
| 1152 | if (arg) { |
| 1153 | st_set_trace_file(arg); |
| 1154 | } |
| 1155 | } else { |
| 1156 | monitor_printf(mon, "unexpected argument \"%s\"\n", op); |
| 1157 | help_cmd(mon, "trace-file"); |
| 1158 | } |
| 1159 | } |
Prerna Saxena | 22890ab | 2010-06-24 17:04:53 +0530 | [diff] [blame] | 1160 | #endif |
| 1161 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 1162 | static void hmp_info_help(Monitor *mon, const QDict *qdict) |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 1163 | { |
Luiz Capitulino | 13c7425 | 2009-10-07 13:41:55 -0300 | [diff] [blame] | 1164 | help_cmd(mon, "info"); |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
Marc-André Lureau | 9e812b6 | 2016-09-12 13:19:05 +0400 | [diff] [blame] | 1167 | static void query_commands_cb(QmpCommand *cmd, void *opaque) |
bellard | 9bc9d1c | 2004-10-10 15:15:51 +0000 | [diff] [blame] | 1168 | { |
Marc-André Lureau | 9e812b6 | 2016-09-12 13:19:05 +0400 | [diff] [blame] | 1169 | CommandInfoList *info, **list = opaque; |
Luiz Capitulino | e3bba9d | 2009-11-26 22:58:56 -0200 | [diff] [blame] | 1170 | |
Marc-André Lureau | 9e812b6 | 2016-09-12 13:19:05 +0400 | [diff] [blame] | 1171 | if (!cmd->enabled) { |
| 1172 | return; |
Luiz Capitulino | e3bba9d | 2009-11-26 22:58:56 -0200 | [diff] [blame] | 1173 | } |
| 1174 | |
Marc-André Lureau | 9e812b6 | 2016-09-12 13:19:05 +0400 | [diff] [blame] | 1175 | info = g_malloc0(sizeof(*info)); |
| 1176 | info->value = g_malloc0(sizeof(*info->value)); |
| 1177 | info->value->name = g_strdup(cmd->name); |
| 1178 | info->next = *list; |
| 1179 | *list = info; |
| 1180 | } |
| 1181 | |
| 1182 | CommandInfoList *qmp_query_commands(Error **errp) |
| 1183 | { |
| 1184 | CommandInfoList *list = NULL; |
| 1185 | |
Markus Armbruster | 635db18 | 2017-03-03 13:32:27 +0100 | [diff] [blame] | 1186 | qmp_for_each_command(cur_mon->qmp.commands, query_commands_cb, &list); |
Marc-André Lureau | 9e812b6 | 2016-09-12 13:19:05 +0400 | [diff] [blame] | 1187 | |
| 1188 | return list; |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
Daniel P. Berrange | 4860853 | 2012-05-21 17:59:51 +0100 | [diff] [blame] | 1191 | EventInfoList *qmp_query_events(Error **errp) |
| 1192 | { |
| 1193 | EventInfoList *info, *ev_list = NULL; |
Wenchao Xia | 7517517 | 2014-06-18 08:43:54 +0200 | [diff] [blame] | 1194 | QAPIEvent e; |
Daniel P. Berrange | 4860853 | 2012-05-21 17:59:51 +0100 | [diff] [blame] | 1195 | |
Eric Blake | 7fb1cf1 | 2015-11-18 01:52:57 -0700 | [diff] [blame] | 1196 | for (e = 0 ; e < QAPI_EVENT__MAX ; e++) { |
Markus Armbruster | 977c736 | 2017-08-24 10:46:08 +0200 | [diff] [blame] | 1197 | const char *event_name = QAPIEvent_str(e); |
Daniel P. Berrange | 4860853 | 2012-05-21 17:59:51 +0100 | [diff] [blame] | 1198 | assert(event_name != NULL); |
| 1199 | info = g_malloc0(sizeof(*info)); |
| 1200 | info->value = g_malloc0(sizeof(*info->value)); |
| 1201 | info->value->name = g_strdup(event_name); |
| 1202 | |
| 1203 | info->next = ev_list; |
| 1204 | ev_list = info; |
| 1205 | } |
| 1206 | |
| 1207 | return ev_list; |
| 1208 | } |
| 1209 | |
Markus Armbruster | 39a1815 | 2015-09-16 13:06:28 +0200 | [diff] [blame] | 1210 | /* |
| 1211 | * Minor hack: generated marshalling suppressed for this command |
| 1212 | * ('gen': false in the schema) so we can parse the JSON string |
| 1213 | * directly into QObject instead of first parsing it with |
| 1214 | * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it |
| 1215 | * to QObject with generated output marshallers, every time. Instead, |
Daniel P. Berrange | b3db211 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 1216 | * we do it in test-qobject-input-visitor.c, just to make sure |
Markus Armbruster | fb0bc83 | 2018-02-26 13:48:58 -0600 | [diff] [blame] | 1217 | * qapi-gen.py's output actually conforms to the schema. |
Markus Armbruster | 39a1815 | 2015-09-16 13:06:28 +0200 | [diff] [blame] | 1218 | */ |
| 1219 | static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data, |
| 1220 | Error **errp) |
| 1221 | { |
Marc-André Lureau | 7d0f982 | 2018-03-05 18:29:51 +0100 | [diff] [blame] | 1222 | *ret_data = qobject_from_qlit(&qmp_schema_qlit); |
Markus Armbruster | 39a1815 | 2015-09-16 13:06:28 +0200 | [diff] [blame] | 1223 | } |
| 1224 | |
Marc-André Lureau | 5032a16 | 2016-09-12 13:19:02 +0400 | [diff] [blame] | 1225 | /* |
Marc-André Lureau | 5032a16 | 2016-09-12 13:19:02 +0400 | [diff] [blame] | 1226 | * We used to define commands in qmp-commands.hx in addition to the |
| 1227 | * QAPI schema. This permitted defining some of them only in certain |
| 1228 | * configurations. query-commands has always reflected that (good, |
| 1229 | * because it lets QMP clients figure out what's actually available), |
| 1230 | * while query-qmp-schema never did (not so good). This function is a |
| 1231 | * hack to keep the configuration-specific commands defined exactly as |
| 1232 | * before, even though qmp-commands.hx is gone. |
| 1233 | * |
| 1234 | * FIXME Educate the QAPI schema on configuration-specific commands, |
| 1235 | * and drop this hack. |
| 1236 | */ |
| 1237 | static void qmp_unregister_commands_hack(void) |
| 1238 | { |
Markus Armbruster | 38bb54f | 2017-04-27 15:00:53 +0200 | [diff] [blame] | 1239 | #ifndef CONFIG_REPLICATION |
| 1240 | qmp_unregister_command(&qmp_commands, "xen-set-replication"); |
| 1241 | qmp_unregister_command(&qmp_commands, "query-xen-replication-status"); |
| 1242 | qmp_unregister_command(&qmp_commands, "xen-colo-do-checkpoint"); |
| 1243 | #endif |
Marc-André Lureau | 5032a16 | 2016-09-12 13:19:02 +0400 | [diff] [blame] | 1244 | #ifndef TARGET_I386 |
Markus Armbruster | 1527bad | 2017-03-03 13:32:25 +0100 | [diff] [blame] | 1245 | qmp_unregister_command(&qmp_commands, "rtc-reset-reinjection"); |
Brijesh Singh | 08a161f | 2018-03-08 06:48:42 -0600 | [diff] [blame] | 1246 | qmp_unregister_command(&qmp_commands, "query-sev"); |
Brijesh Singh | 1b6a034 | 2018-03-08 06:48:56 -0600 | [diff] [blame] | 1247 | qmp_unregister_command(&qmp_commands, "query-sev-launch-measure"); |
Brijesh Singh | 31dd67f | 2018-03-08 06:48:59 -0600 | [diff] [blame] | 1248 | qmp_unregister_command(&qmp_commands, "query-sev-capabilities"); |
Marc-André Lureau | 5032a16 | 2016-09-12 13:19:02 +0400 | [diff] [blame] | 1249 | #endif |
| 1250 | #ifndef TARGET_S390X |
Markus Armbruster | 1527bad | 2017-03-03 13:32:25 +0100 | [diff] [blame] | 1251 | qmp_unregister_command(&qmp_commands, "dump-skeys"); |
Marc-André Lureau | 5032a16 | 2016-09-12 13:19:02 +0400 | [diff] [blame] | 1252 | #endif |
| 1253 | #ifndef TARGET_ARM |
Markus Armbruster | 1527bad | 2017-03-03 13:32:25 +0100 | [diff] [blame] | 1254 | qmp_unregister_command(&qmp_commands, "query-gic-capabilities"); |
Marc-André Lureau | 5032a16 | 2016-09-12 13:19:02 +0400 | [diff] [blame] | 1255 | #endif |
Eduardo Habkost | f99fd7c | 2017-02-22 16:00:28 -0300 | [diff] [blame] | 1256 | #if !defined(TARGET_S390X) && !defined(TARGET_I386) |
Markus Armbruster | 1527bad | 2017-03-03 13:32:25 +0100 | [diff] [blame] | 1257 | qmp_unregister_command(&qmp_commands, "query-cpu-model-expansion"); |
Eduardo Habkost | f99fd7c | 2017-02-22 16:00:28 -0300 | [diff] [blame] | 1258 | #endif |
| 1259 | #if !defined(TARGET_S390X) |
Markus Armbruster | 1527bad | 2017-03-03 13:32:25 +0100 | [diff] [blame] | 1260 | qmp_unregister_command(&qmp_commands, "query-cpu-model-baseline"); |
| 1261 | qmp_unregister_command(&qmp_commands, "query-cpu-model-comparison"); |
Eduardo Habkost | 728b142 | 2016-10-05 16:49:01 -0300 | [diff] [blame] | 1262 | #endif |
| 1263 | #if !defined(TARGET_PPC) && !defined(TARGET_ARM) && !defined(TARGET_I386) \ |
| 1264 | && !defined(TARGET_S390X) |
Markus Armbruster | 1527bad | 2017-03-03 13:32:25 +0100 | [diff] [blame] | 1265 | qmp_unregister_command(&qmp_commands, "query-cpu-definitions"); |
Eduardo Habkost | 728b142 | 2016-10-05 16:49:01 -0300 | [diff] [blame] | 1266 | #endif |
Marc-André Lureau | 5032a16 | 2016-09-12 13:19:02 +0400 | [diff] [blame] | 1267 | } |
| 1268 | |
Peter Xu | 6adf08d | 2018-03-09 16:59:50 +0800 | [diff] [blame] | 1269 | static void monitor_init_qmp_commands(void) |
Marc-André Lureau | edcfaef | 2016-09-12 13:19:00 +0400 | [diff] [blame] | 1270 | { |
Markus Armbruster | 635db18 | 2017-03-03 13:32:27 +0100 | [diff] [blame] | 1271 | /* |
| 1272 | * Two command lists: |
| 1273 | * - qmp_commands contains all QMP commands |
| 1274 | * - qmp_cap_negotiation_commands contains just |
| 1275 | * "qmp_capabilities", to enforce capability negotiation |
| 1276 | */ |
| 1277 | |
Markus Armbruster | 1527bad | 2017-03-03 13:32:25 +0100 | [diff] [blame] | 1278 | qmp_init_marshal(&qmp_commands); |
Markus Armbruster | 0587568 | 2017-03-03 13:32:24 +0100 | [diff] [blame] | 1279 | |
Markus Armbruster | 1527bad | 2017-03-03 13:32:25 +0100 | [diff] [blame] | 1280 | qmp_register_command(&qmp_commands, "query-qmp-schema", |
Igor Mammedov | d6fe3d0 | 2018-05-11 18:51:43 +0200 | [diff] [blame] | 1281 | qmp_query_qmp_schema, QCO_ALLOW_PRECONFIG); |
Markus Armbruster | 1527bad | 2017-03-03 13:32:25 +0100 | [diff] [blame] | 1282 | qmp_register_command(&qmp_commands, "device_add", qmp_device_add, |
Marc-André Lureau | edcfaef | 2016-09-12 13:19:00 +0400 | [diff] [blame] | 1283 | QCO_NO_OPTIONS); |
Markus Armbruster | 1527bad | 2017-03-03 13:32:25 +0100 | [diff] [blame] | 1284 | qmp_register_command(&qmp_commands, "netdev_add", qmp_netdev_add, |
Marc-André Lureau | edcfaef | 2016-09-12 13:19:00 +0400 | [diff] [blame] | 1285 | QCO_NO_OPTIONS); |
Marc-André Lureau | 5032a16 | 2016-09-12 13:19:02 +0400 | [diff] [blame] | 1286 | |
Markus Armbruster | 0587568 | 2017-03-03 13:32:24 +0100 | [diff] [blame] | 1287 | qmp_unregister_commands_hack(); |
Markus Armbruster | 635db18 | 2017-03-03 13:32:27 +0100 | [diff] [blame] | 1288 | |
| 1289 | QTAILQ_INIT(&qmp_cap_negotiation_commands); |
| 1290 | qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities", |
Igor Mammedov | d6fe3d0 | 2018-05-11 18:51:43 +0200 | [diff] [blame] | 1291 | qmp_marshal_qmp_capabilities, QCO_ALLOW_PRECONFIG); |
Markus Armbruster | 635db18 | 2017-03-03 13:32:27 +0100 | [diff] [blame] | 1292 | } |
| 1293 | |
Peter Xu | 71da466 | 2018-03-09 16:59:57 +0800 | [diff] [blame] | 1294 | static bool qmp_oob_enabled(Monitor *mon) |
| 1295 | { |
Markus Armbruster | 279f9e0 | 2018-07-03 10:53:56 +0200 | [diff] [blame] | 1296 | return mon->qmp.capab[QMP_CAPABILITY_OOB]; |
Peter Xu | 71da466 | 2018-03-09 16:59:57 +0800 | [diff] [blame] | 1297 | } |
| 1298 | |
Markus Armbruster | 279f9e0 | 2018-07-03 10:53:56 +0200 | [diff] [blame] | 1299 | static void monitor_qmp_caps_reset(Monitor *mon) |
Markus Armbruster | 635db18 | 2017-03-03 13:32:27 +0100 | [diff] [blame] | 1300 | { |
Markus Armbruster | 279f9e0 | 2018-07-03 10:53:56 +0200 | [diff] [blame] | 1301 | memset(mon->qmp.capab_offered, 0, sizeof(mon->qmp.capab_offered)); |
| 1302 | memset(mon->qmp.capab, 0, sizeof(mon->qmp.capab)); |
| 1303 | mon->qmp.capab_offered[QMP_CAPABILITY_OOB] = mon->use_io_thread; |
Peter Xu | 0213031 | 2018-03-09 16:59:53 +0800 | [diff] [blame] | 1304 | } |
| 1305 | |
Peter Xu | cf869d5 | 2018-03-10 20:38:05 -0600 | [diff] [blame] | 1306 | /* |
Markus Armbruster | 279f9e0 | 2018-07-03 10:53:56 +0200 | [diff] [blame] | 1307 | * Accept QMP capabilities in @list for @mon. |
| 1308 | * On success, set mon->qmp.capab[], and return true. |
| 1309 | * On error, set @errp, and return false. |
Peter Xu | cf869d5 | 2018-03-10 20:38:05 -0600 | [diff] [blame] | 1310 | */ |
Markus Armbruster | 279f9e0 | 2018-07-03 10:53:56 +0200 | [diff] [blame] | 1311 | static bool qmp_caps_accept(Monitor *mon, QMPCapabilityList *list, |
| 1312 | Error **errp) |
Peter Xu | cf869d5 | 2018-03-10 20:38:05 -0600 | [diff] [blame] | 1313 | { |
Markus Armbruster | 279f9e0 | 2018-07-03 10:53:56 +0200 | [diff] [blame] | 1314 | GString *unavailable = NULL; |
| 1315 | bool capab[QMP_CAPABILITY__MAX]; |
Peter Xu | cf869d5 | 2018-03-10 20:38:05 -0600 | [diff] [blame] | 1316 | |
Markus Armbruster | 279f9e0 | 2018-07-03 10:53:56 +0200 | [diff] [blame] | 1317 | memset(capab, 0, sizeof(capab)); |
| 1318 | |
Peter Xu | 0213031 | 2018-03-09 16:59:53 +0800 | [diff] [blame] | 1319 | for (; list; list = list->next) { |
Markus Armbruster | 279f9e0 | 2018-07-03 10:53:56 +0200 | [diff] [blame] | 1320 | if (!mon->qmp.capab_offered[list->value]) { |
| 1321 | if (!unavailable) { |
| 1322 | unavailable = g_string_new(QMPCapability_str(list->value)); |
| 1323 | } else { |
| 1324 | g_string_append_printf(unavailable, ", %s", |
| 1325 | QMPCapability_str(list->value)); |
Peter Xu | 0213031 | 2018-03-09 16:59:53 +0800 | [diff] [blame] | 1326 | } |
Peter Xu | 0213031 | 2018-03-09 16:59:53 +0800 | [diff] [blame] | 1327 | } |
Markus Armbruster | 279f9e0 | 2018-07-03 10:53:56 +0200 | [diff] [blame] | 1328 | capab[list->value] = true; |
Peter Xu | 0213031 | 2018-03-09 16:59:53 +0800 | [diff] [blame] | 1329 | } |
Peter Xu | 0213031 | 2018-03-09 16:59:53 +0800 | [diff] [blame] | 1330 | |
Markus Armbruster | 279f9e0 | 2018-07-03 10:53:56 +0200 | [diff] [blame] | 1331 | if (unavailable) { |
| 1332 | error_setg(errp, "Capability %s not available", unavailable->str); |
| 1333 | g_string_free(unavailable, true); |
Peter Xu | cf869d5 | 2018-03-10 20:38:05 -0600 | [diff] [blame] | 1334 | return false; |
| 1335 | } |
| 1336 | |
Markus Armbruster | 279f9e0 | 2018-07-03 10:53:56 +0200 | [diff] [blame] | 1337 | memcpy(mon->qmp.capab, capab, sizeof(capab)); |
Peter Xu | cf869d5 | 2018-03-10 20:38:05 -0600 | [diff] [blame] | 1338 | return true; |
| 1339 | } |
| 1340 | |
Peter Xu | 0213031 | 2018-03-09 16:59:53 +0800 | [diff] [blame] | 1341 | void qmp_qmp_capabilities(bool has_enable, QMPCapabilityList *enable, |
| 1342 | Error **errp) |
| 1343 | { |
Markus Armbruster | 635db18 | 2017-03-03 13:32:27 +0100 | [diff] [blame] | 1344 | if (cur_mon->qmp.commands == &qmp_commands) { |
| 1345 | error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND, |
| 1346 | "Capabilities negotiation is already complete, command " |
| 1347 | "ignored"); |
| 1348 | return; |
| 1349 | } |
| 1350 | |
Markus Armbruster | 279f9e0 | 2018-07-03 10:53:56 +0200 | [diff] [blame] | 1351 | if (!qmp_caps_accept(cur_mon, enable, errp)) { |
| 1352 | return; |
Peter Xu | 0213031 | 2018-03-09 16:59:53 +0800 | [diff] [blame] | 1353 | } |
| 1354 | |
Markus Armbruster | 635db18 | 2017-03-03 13:32:27 +0100 | [diff] [blame] | 1355 | cur_mon->qmp.commands = &qmp_commands; |
Marc-André Lureau | edcfaef | 2016-09-12 13:19:00 +0400 | [diff] [blame] | 1356 | } |
| 1357 | |
Peter Xu | d9f2528 | 2018-06-08 11:55:07 +0800 | [diff] [blame] | 1358 | /* Set the current CPU defined by the user. Callers must hold BQL. */ |
Luiz Capitulino | b025c8b | 2011-10-06 14:02:57 -0300 | [diff] [blame] | 1359 | int monitor_set_cpu(int cpu_index) |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 1360 | { |
Andreas Färber | 55e5c28 | 2012-12-17 06:18:02 +0100 | [diff] [blame] | 1361 | CPUState *cpu; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 1362 | |
Andreas Färber | 1c8bb3c | 2013-02-15 17:01:09 +0100 | [diff] [blame] | 1363 | cpu = qemu_get_cpu(cpu_index); |
| 1364 | if (cpu == NULL) { |
| 1365 | return -1; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 1366 | } |
Greg Kurz | 751f8cf | 2017-10-17 10:16:22 +0200 | [diff] [blame] | 1367 | g_free(cur_mon->mon_cpu_path); |
| 1368 | cur_mon->mon_cpu_path = object_get_canonical_path(OBJECT(cpu)); |
Andreas Färber | 1c8bb3c | 2013-02-15 17:01:09 +0100 | [diff] [blame] | 1369 | return 0; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 1370 | } |
| 1371 | |
Peter Xu | d9f2528 | 2018-06-08 11:55:07 +0800 | [diff] [blame] | 1372 | /* Callers must hold BQL. */ |
Viktor Mihajlovski | 137b5cb | 2018-02-16 17:08:41 +0100 | [diff] [blame] | 1373 | static CPUState *mon_get_cpu_sync(bool synchronize) |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 1374 | { |
Greg Kurz | 751f8cf | 2017-10-17 10:16:22 +0200 | [diff] [blame] | 1375 | CPUState *cpu; |
| 1376 | |
| 1377 | if (cur_mon->mon_cpu_path) { |
| 1378 | cpu = (CPUState *) object_resolve_path_type(cur_mon->mon_cpu_path, |
| 1379 | TYPE_CPU, NULL); |
| 1380 | if (!cpu) { |
| 1381 | g_free(cur_mon->mon_cpu_path); |
| 1382 | cur_mon->mon_cpu_path = NULL; |
| 1383 | } |
| 1384 | } |
| 1385 | if (!cur_mon->mon_cpu_path) { |
Thomas Huth | 854e67f | 2017-01-13 13:12:35 +0100 | [diff] [blame] | 1386 | if (!first_cpu) { |
| 1387 | return NULL; |
| 1388 | } |
David Gibson | 27a83f8 | 2016-09-21 15:29:26 +1000 | [diff] [blame] | 1389 | monitor_set_cpu(first_cpu->cpu_index); |
Greg Kurz | 751f8cf | 2017-10-17 10:16:22 +0200 | [diff] [blame] | 1390 | cpu = first_cpu; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 1391 | } |
Viktor Mihajlovski | 137b5cb | 2018-02-16 17:08:41 +0100 | [diff] [blame] | 1392 | if (synchronize) { |
| 1393 | cpu_synchronize_state(cpu); |
| 1394 | } |
Greg Kurz | 751f8cf | 2017-10-17 10:16:22 +0200 | [diff] [blame] | 1395 | return cpu; |
Peter Crosthwaite | 5bcda5f | 2015-05-24 14:20:40 -0700 | [diff] [blame] | 1396 | } |
| 1397 | |
Viktor Mihajlovski | 137b5cb | 2018-02-16 17:08:41 +0100 | [diff] [blame] | 1398 | CPUState *mon_get_cpu(void) |
| 1399 | { |
| 1400 | return mon_get_cpu_sync(true); |
| 1401 | } |
| 1402 | |
Pavel Butsykin | bf95728 | 2015-09-10 18:38:59 +0300 | [diff] [blame] | 1403 | CPUArchState *mon_get_cpu_env(void) |
Peter Crosthwaite | 5bcda5f | 2015-05-24 14:20:40 -0700 | [diff] [blame] | 1404 | { |
Thomas Huth | 854e67f | 2017-01-13 13:12:35 +0100 | [diff] [blame] | 1405 | CPUState *cs = mon_get_cpu(); |
| 1406 | |
| 1407 | return cs ? cs->env_ptr : NULL; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 1408 | } |
| 1409 | |
Luiz Capitulino | 99b7796 | 2011-10-24 10:53:44 -0200 | [diff] [blame] | 1410 | int monitor_get_cpu_index(void) |
| 1411 | { |
Viktor Mihajlovski | 137b5cb | 2018-02-16 17:08:41 +0100 | [diff] [blame] | 1412 | CPUState *cs = mon_get_cpu_sync(false); |
Thomas Huth | 854e67f | 2017-01-13 13:12:35 +0100 | [diff] [blame] | 1413 | |
| 1414 | return cs ? cs->cpu_index : UNASSIGNED_CPU_INDEX; |
Luiz Capitulino | 99b7796 | 2011-10-24 10:53:44 -0200 | [diff] [blame] | 1415 | } |
| 1416 | |
Markus Armbruster | 1ce6be2 | 2015-02-06 14:18:24 +0100 | [diff] [blame] | 1417 | static void hmp_info_registers(Monitor *mon, const QDict *qdict) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1418 | { |
Suraj Jitindar Singh | 18f0828 | 2017-06-08 15:41:16 +1000 | [diff] [blame] | 1419 | bool all_cpus = qdict_get_try_bool(qdict, "cpustate_all", false); |
| 1420 | CPUState *cs; |
Thomas Huth | 854e67f | 2017-01-13 13:12:35 +0100 | [diff] [blame] | 1421 | |
Suraj Jitindar Singh | 18f0828 | 2017-06-08 15:41:16 +1000 | [diff] [blame] | 1422 | if (all_cpus) { |
| 1423 | CPU_FOREACH(cs) { |
| 1424 | monitor_printf(mon, "\nCPU#%d\n", cs->cpu_index); |
| 1425 | cpu_dump_state(cs, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU); |
| 1426 | } |
| 1427 | } else { |
| 1428 | cs = mon_get_cpu(); |
| 1429 | |
| 1430 | if (!cs) { |
| 1431 | monitor_printf(mon, "No CPU available\n"); |
| 1432 | return; |
| 1433 | } |
| 1434 | |
| 1435 | cpu_dump_state(cs, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU); |
Thomas Huth | 854e67f | 2017-01-13 13:12:35 +0100 | [diff] [blame] | 1436 | } |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
Paolo Bonzini | f0d14a9 | 2012-09-17 13:42:41 +0200 | [diff] [blame] | 1439 | #ifdef CONFIG_TCG |
Markus Armbruster | 1ce6be2 | 2015-02-06 14:18:24 +0100 | [diff] [blame] | 1440 | static void hmp_info_jit(Monitor *mon, const QDict *qdict) |
bellard | e3db722 | 2005-01-26 22:00:47 +0000 | [diff] [blame] | 1441 | { |
Thomas Huth | b7da97e | 2017-04-26 06:11:47 +0200 | [diff] [blame] | 1442 | if (!tcg_enabled()) { |
| 1443 | error_report("JIT information is only available with accel=tcg"); |
| 1444 | return; |
| 1445 | } |
| 1446 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1447 | dump_exec_info((FILE *)mon, monitor_fprintf); |
Sebastian Tanase | 27498be | 2014-07-25 11:56:33 +0200 | [diff] [blame] | 1448 | dump_drift_info((FILE *)mon, monitor_fprintf); |
bellard | e3db722 | 2005-01-26 22:00:47 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
Markus Armbruster | 1ce6be2 | 2015-02-06 14:18:24 +0100 | [diff] [blame] | 1451 | static void hmp_info_opcount(Monitor *mon, const QDict *qdict) |
Max Filippov | 246ae24 | 2014-11-02 11:04:18 +0300 | [diff] [blame] | 1452 | { |
| 1453 | dump_opcount_info((FILE *)mon, monitor_fprintf); |
| 1454 | } |
Paolo Bonzini | f0d14a9 | 2012-09-17 13:42:41 +0200 | [diff] [blame] | 1455 | #endif |
Max Filippov | 246ae24 | 2014-11-02 11:04:18 +0300 | [diff] [blame] | 1456 | |
Markus Armbruster | 1ce6be2 | 2015-02-06 14:18:24 +0100 | [diff] [blame] | 1457 | static void hmp_info_history(Monitor *mon, const QDict *qdict) |
bellard | aa45548 | 2004-04-04 13:07:25 +0000 | [diff] [blame] | 1458 | { |
| 1459 | int i; |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 1460 | const char *str; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1461 | |
aliguori | cde76ee | 2009-03-05 23:01:51 +0000 | [diff] [blame] | 1462 | if (!mon->rs) |
| 1463 | return; |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 1464 | i = 0; |
| 1465 | for(;;) { |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 1466 | str = readline_get_history(mon->rs, i); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 1467 | if (!str) |
| 1468 | break; |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1469 | monitor_printf(mon, "%d: '%s'\n", i, str); |
bellard | 8e3a9fd | 2004-10-09 17:32:58 +0000 | [diff] [blame] | 1470 | i++; |
bellard | aa45548 | 2004-04-04 13:07:25 +0000 | [diff] [blame] | 1471 | } |
| 1472 | } |
| 1473 | |
Markus Armbruster | 1ce6be2 | 2015-02-06 14:18:24 +0100 | [diff] [blame] | 1474 | static void hmp_info_cpustats(Monitor *mon, const QDict *qdict) |
j_mayer | 76a6625 | 2007-03-07 08:32:30 +0000 | [diff] [blame] | 1475 | { |
Thomas Huth | 854e67f | 2017-01-13 13:12:35 +0100 | [diff] [blame] | 1476 | CPUState *cs = mon_get_cpu(); |
| 1477 | |
| 1478 | if (!cs) { |
| 1479 | monitor_printf(mon, "No CPU available\n"); |
| 1480 | return; |
| 1481 | } |
| 1482 | cpu_dump_statistics(cs, (FILE *)mon, &monitor_fprintf, 0); |
j_mayer | 76a6625 | 2007-03-07 08:32:30 +0000 | [diff] [blame] | 1483 | } |
j_mayer | 76a6625 | 2007-03-07 08:32:30 +0000 | [diff] [blame] | 1484 | |
Markus Armbruster | 1ce6be2 | 2015-02-06 14:18:24 +0100 | [diff] [blame] | 1485 | static void hmp_info_trace_events(Monitor *mon, const QDict *qdict) |
Prerna Saxena | 22890ab | 2010-06-24 17:04:53 +0530 | [diff] [blame] | 1486 | { |
Lluís Vilanova | bd71211 | 2016-07-11 12:53:51 +0200 | [diff] [blame] | 1487 | const char *name = qdict_get_try_str(qdict, "name"); |
Lluís Vilanova | 77e2b17 | 2016-07-11 12:53:57 +0200 | [diff] [blame] | 1488 | bool has_vcpu = qdict_haskey(qdict, "vcpu"); |
| 1489 | int vcpu = qdict_get_try_int(qdict, "vcpu", 0); |
Lluís Vilanova | bd71211 | 2016-07-11 12:53:51 +0200 | [diff] [blame] | 1490 | TraceEventInfoList *events; |
Lluís Vilanova | 14101d0 | 2014-08-25 13:20:03 +0200 | [diff] [blame] | 1491 | TraceEventInfoList *elem; |
Lluís Vilanova | bd71211 | 2016-07-11 12:53:51 +0200 | [diff] [blame] | 1492 | Error *local_err = NULL; |
| 1493 | |
| 1494 | if (name == NULL) { |
| 1495 | name = "*"; |
| 1496 | } |
Lluís Vilanova | 77e2b17 | 2016-07-11 12:53:57 +0200 | [diff] [blame] | 1497 | if (vcpu < 0) { |
| 1498 | monitor_printf(mon, "argument vcpu must be positive"); |
| 1499 | return; |
| 1500 | } |
Lluís Vilanova | bd71211 | 2016-07-11 12:53:51 +0200 | [diff] [blame] | 1501 | |
Lluís Vilanova | 77e2b17 | 2016-07-11 12:53:57 +0200 | [diff] [blame] | 1502 | events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err); |
Lluís Vilanova | bd71211 | 2016-07-11 12:53:51 +0200 | [diff] [blame] | 1503 | if (local_err) { |
| 1504 | error_report_err(local_err); |
| 1505 | return; |
| 1506 | } |
Lluís Vilanova | 14101d0 | 2014-08-25 13:20:03 +0200 | [diff] [blame] | 1507 | |
| 1508 | for (elem = events; elem != NULL; elem = elem->next) { |
| 1509 | monitor_printf(mon, "%s : state %u\n", |
| 1510 | elem->value->name, |
| 1511 | elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0); |
| 1512 | } |
| 1513 | qapi_free_TraceEventInfoList(events); |
Prerna Saxena | 22890ab | 2010-06-24 17:04:53 +0530 | [diff] [blame] | 1514 | } |
Prerna Saxena | 22890ab | 2010-06-24 17:04:53 +0530 | [diff] [blame] | 1515 | |
Markus Armbruster | b8a185b | 2015-03-05 17:29:02 +0100 | [diff] [blame] | 1516 | void qmp_client_migrate_info(const char *protocol, const char *hostname, |
| 1517 | bool has_port, int64_t port, |
| 1518 | bool has_tls_port, int64_t tls_port, |
| 1519 | bool has_cert_subject, const char *cert_subject, |
| 1520 | Error **errp) |
Gerd Hoffmann | e866e23 | 2010-04-23 13:28:21 +0200 | [diff] [blame] | 1521 | { |
Gerd Hoffmann | e866e23 | 2010-04-23 13:28:21 +0200 | [diff] [blame] | 1522 | if (strcmp(protocol, "spice") == 0) { |
Markus Armbruster | b8a185b | 2015-03-05 17:29:02 +0100 | [diff] [blame] | 1523 | if (!qemu_using_spice(errp)) { |
| 1524 | return; |
Gerd Hoffmann | e866e23 | 2010-04-23 13:28:21 +0200 | [diff] [blame] | 1525 | } |
| 1526 | |
Markus Armbruster | b8a185b | 2015-03-05 17:29:02 +0100 | [diff] [blame] | 1527 | if (!has_port && !has_tls_port) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 1528 | error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port"); |
Markus Armbruster | b8a185b | 2015-03-05 17:29:02 +0100 | [diff] [blame] | 1529 | return; |
Yonit Halperin | 6ec5dae | 2012-03-18 09:42:39 +0200 | [diff] [blame] | 1530 | } |
| 1531 | |
Markus Armbruster | b8a185b | 2015-03-05 17:29:02 +0100 | [diff] [blame] | 1532 | if (qemu_spice_migrate_info(hostname, |
| 1533 | has_port ? port : -1, |
| 1534 | has_tls_port ? tls_port : -1, |
| 1535 | cert_subject)) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 1536 | error_setg(errp, QERR_UNDEFINED_ERROR); |
Markus Armbruster | b8a185b | 2015-03-05 17:29:02 +0100 | [diff] [blame] | 1537 | return; |
Gerd Hoffmann | e866e23 | 2010-04-23 13:28:21 +0200 | [diff] [blame] | 1538 | } |
Markus Armbruster | b8a185b | 2015-03-05 17:29:02 +0100 | [diff] [blame] | 1539 | return; |
Gerd Hoffmann | e866e23 | 2010-04-23 13:28:21 +0200 | [diff] [blame] | 1540 | } |
| 1541 | |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 1542 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice"); |
Gerd Hoffmann | e866e23 | 2010-04-23 13:28:21 +0200 | [diff] [blame] | 1543 | } |
| 1544 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 1545 | static void hmp_logfile(Monitor *mon, const QDict *qdict) |
pbrook | e735b91 | 2007-06-30 13:53:24 +0000 | [diff] [blame] | 1546 | { |
Markus Armbruster | daa76aa | 2016-06-15 19:27:16 +0200 | [diff] [blame] | 1547 | Error *err = NULL; |
| 1548 | |
| 1549 | qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err); |
| 1550 | if (err) { |
| 1551 | error_report_err(err); |
| 1552 | } |
pbrook | e735b91 | 2007-06-30 13:53:24 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 1555 | static void hmp_log(Monitor *mon, const QDict *qdict) |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1556 | { |
| 1557 | int mask; |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 1558 | const char *items = qdict_get_str(qdict, "items"); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1559 | |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1560 | if (!strcmp(items, "none")) { |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1561 | mask = 0; |
| 1562 | } else { |
Peter Maydell | 4fde1eb | 2013-02-11 16:41:22 +0000 | [diff] [blame] | 1563 | mask = qemu_str_to_log_mask(items); |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1564 | if (!mask) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1565 | help_cmd(mon, "log"); |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1566 | return; |
| 1567 | } |
| 1568 | } |
Peter Maydell | 24537a0 | 2013-02-11 16:41:23 +0000 | [diff] [blame] | 1569 | qemu_set_log(mask); |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1570 | } |
| 1571 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 1572 | static void hmp_singlestep(Monitor *mon, const QDict *qdict) |
aurel32 | 1b530a6 | 2009-04-05 20:08:59 +0000 | [diff] [blame] | 1573 | { |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 1574 | const char *option = qdict_get_try_str(qdict, "option"); |
aurel32 | 1b530a6 | 2009-04-05 20:08:59 +0000 | [diff] [blame] | 1575 | if (!option || !strcmp(option, "on")) { |
| 1576 | singlestep = 1; |
| 1577 | } else if (!strcmp(option, "off")) { |
| 1578 | singlestep = 0; |
| 1579 | } else { |
| 1580 | monitor_printf(mon, "unexpected option %s\n", option); |
| 1581 | } |
| 1582 | } |
| 1583 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 1584 | static void hmp_gdbserver(Monitor *mon, const QDict *qdict) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1585 | { |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 1586 | const char *device = qdict_get_try_str(qdict, "device"); |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 1587 | if (!device) |
| 1588 | device = "tcp::" DEFAULT_GDBSTUB_PORT; |
| 1589 | if (gdbserver_start(device) < 0) { |
| 1590 | monitor_printf(mon, "Could not open gdbserver on device '%s'\n", |
| 1591 | device); |
| 1592 | } else if (strcmp(device, "none") == 0) { |
aliguori | 36556b2 | 2009-03-28 18:05:53 +0000 | [diff] [blame] | 1593 | monitor_printf(mon, "Disabled gdbserver\n"); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1594 | } else { |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 1595 | monitor_printf(mon, "Waiting for gdb connection on device '%s'\n", |
| 1596 | device); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1597 | } |
| 1598 | } |
| 1599 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 1600 | static void hmp_watchdog_action(Monitor *mon, const QDict *qdict) |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 1601 | { |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 1602 | const char *action = qdict_get_str(qdict, "action"); |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 1603 | if (select_watchdog_action(action) == -1) { |
| 1604 | monitor_printf(mon, "Unknown watchdog action '%s'\n", action); |
| 1605 | } |
| 1606 | } |
| 1607 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1608 | static void monitor_printc(Monitor *mon, int c) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1609 | { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1610 | monitor_printf(mon, "'"); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1611 | switch(c) { |
| 1612 | case '\'': |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1613 | monitor_printf(mon, "\\'"); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1614 | break; |
| 1615 | case '\\': |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1616 | monitor_printf(mon, "\\\\"); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1617 | break; |
| 1618 | case '\n': |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1619 | monitor_printf(mon, "\\n"); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1620 | break; |
| 1621 | case '\r': |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1622 | monitor_printf(mon, "\\r"); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1623 | break; |
| 1624 | default: |
| 1625 | if (c >= 32 && c <= 126) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1626 | monitor_printf(mon, "%c", c); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1627 | } else { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1628 | monitor_printf(mon, "\\x%02x", c); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1629 | } |
| 1630 | break; |
| 1631 | } |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1632 | monitor_printf(mon, "'"); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1633 | } |
| 1634 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1635 | static void memory_dump(Monitor *mon, int count, int format, int wsize, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1636 | hwaddr addr, int is_physical) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1637 | { |
Blue Swirl | 23842aa | 2010-01-12 20:27:43 +0000 | [diff] [blame] | 1638 | int l, line_size, i, max_digits, len; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1639 | uint8_t buf[16]; |
| 1640 | uint64_t v; |
Thomas Huth | 854e67f | 2017-01-13 13:12:35 +0100 | [diff] [blame] | 1641 | CPUState *cs = mon_get_cpu(); |
| 1642 | |
| 1643 | if (!cs && (format == 'i' || !is_physical)) { |
| 1644 | monitor_printf(mon, "Can not dump without CPU\n"); |
| 1645 | return; |
| 1646 | } |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1647 | |
| 1648 | if (format == 'i') { |
Richard Henderson | 1d48474 | 2017-09-14 08:38:35 -0700 | [diff] [blame] | 1649 | monitor_disas(mon, cs, addr, count, is_physical); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1650 | return; |
| 1651 | } |
| 1652 | |
| 1653 | len = wsize * count; |
| 1654 | if (wsize == 1) |
| 1655 | line_size = 8; |
| 1656 | else |
| 1657 | line_size = 16; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1658 | max_digits = 0; |
| 1659 | |
| 1660 | switch(format) { |
| 1661 | case 'o': |
Marc-André Lureau | 69db8df | 2017-06-22 13:04:16 +0200 | [diff] [blame] | 1662 | max_digits = DIV_ROUND_UP(wsize * 8, 3); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1663 | break; |
| 1664 | default: |
| 1665 | case 'x': |
| 1666 | max_digits = (wsize * 8) / 4; |
| 1667 | break; |
| 1668 | case 'u': |
| 1669 | case 'd': |
Marc-André Lureau | 69db8df | 2017-06-22 13:04:16 +0200 | [diff] [blame] | 1670 | max_digits = DIV_ROUND_UP(wsize * 8 * 10, 33); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1671 | break; |
| 1672 | case 'c': |
| 1673 | wsize = 1; |
| 1674 | break; |
| 1675 | } |
| 1676 | |
| 1677 | while (len > 0) { |
blueswir1 | 7743e58 | 2007-09-24 18:39:04 +0000 | [diff] [blame] | 1678 | if (is_physical) |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1679 | monitor_printf(mon, TARGET_FMT_plx ":", addr); |
blueswir1 | 7743e58 | 2007-09-24 18:39:04 +0000 | [diff] [blame] | 1680 | else |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1681 | monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1682 | l = len; |
| 1683 | if (l > line_size) |
| 1684 | l = line_size; |
| 1685 | if (is_physical) { |
Stefan Weil | 54f7b4a | 2011-04-10 18:23:39 +0200 | [diff] [blame] | 1686 | cpu_physical_memory_read(addr, buf, l); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1687 | } else { |
Thomas Huth | 854e67f | 2017-01-13 13:12:35 +0100 | [diff] [blame] | 1688 | if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1689 | monitor_printf(mon, " Cannot access memory\n"); |
aliguori | c8f79b6 | 2008-08-18 14:00:20 +0000 | [diff] [blame] | 1690 | break; |
| 1691 | } |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1692 | } |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1693 | i = 0; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1694 | while (i < l) { |
| 1695 | switch(wsize) { |
| 1696 | default: |
| 1697 | case 1: |
Peter Maydell | 24e6030 | 2015-01-20 15:19:32 +0000 | [diff] [blame] | 1698 | v = ldub_p(buf + i); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1699 | break; |
| 1700 | case 2: |
Peter Maydell | 24e6030 | 2015-01-20 15:19:32 +0000 | [diff] [blame] | 1701 | v = lduw_p(buf + i); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1702 | break; |
| 1703 | case 4: |
Peter Maydell | 24e6030 | 2015-01-20 15:19:32 +0000 | [diff] [blame] | 1704 | v = (uint32_t)ldl_p(buf + i); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1705 | break; |
| 1706 | case 8: |
Peter Maydell | 24e6030 | 2015-01-20 15:19:32 +0000 | [diff] [blame] | 1707 | v = ldq_p(buf + i); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1708 | break; |
| 1709 | } |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1710 | monitor_printf(mon, " "); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1711 | switch(format) { |
| 1712 | case 'o': |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1713 | monitor_printf(mon, "%#*" PRIo64, max_digits, v); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1714 | break; |
| 1715 | case 'x': |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1716 | monitor_printf(mon, "0x%0*" PRIx64, max_digits, v); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1717 | break; |
| 1718 | case 'u': |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1719 | monitor_printf(mon, "%*" PRIu64, max_digits, v); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1720 | break; |
| 1721 | case 'd': |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1722 | monitor_printf(mon, "%*" PRId64, max_digits, v); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1723 | break; |
| 1724 | case 'c': |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1725 | monitor_printc(mon, v); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1726 | break; |
| 1727 | } |
| 1728 | i += wsize; |
| 1729 | } |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1730 | monitor_printf(mon, "\n"); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1731 | addr += l; |
| 1732 | len -= l; |
| 1733 | } |
| 1734 | } |
| 1735 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 1736 | static void hmp_memory_dump(Monitor *mon, const QDict *qdict) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1737 | { |
Luiz Capitulino | 1bd1442 | 2009-08-28 15:27:17 -0300 | [diff] [blame] | 1738 | int count = qdict_get_int(qdict, "count"); |
| 1739 | int format = qdict_get_int(qdict, "format"); |
| 1740 | int size = qdict_get_int(qdict, "size"); |
| 1741 | target_long addr = qdict_get_int(qdict, "addr"); |
| 1742 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1743 | memory_dump(mon, count, format, size, addr, 0); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1744 | } |
| 1745 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 1746 | static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1747 | { |
Luiz Capitulino | 1bd1442 | 2009-08-28 15:27:17 -0300 | [diff] [blame] | 1748 | int count = qdict_get_int(qdict, "count"); |
| 1749 | int format = qdict_get_int(qdict, "format"); |
| 1750 | int size = qdict_get_int(qdict, "size"); |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1751 | hwaddr addr = qdict_get_int(qdict, "addr"); |
Luiz Capitulino | 1bd1442 | 2009-08-28 15:27:17 -0300 | [diff] [blame] | 1752 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1753 | memory_dump(mon, count, format, size, addr, 1); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
Paolo Bonzini | e962844 | 2017-04-20 15:30:58 +0200 | [diff] [blame] | 1756 | static void *gpa2hva(MemoryRegion **p_mr, hwaddr addr, Error **errp) |
| 1757 | { |
| 1758 | MemoryRegionSection mrs = memory_region_find(get_system_memory(), |
| 1759 | addr, 1); |
| 1760 | |
| 1761 | if (!mrs.mr) { |
| 1762 | error_setg(errp, "No memory is mapped at address 0x%" HWADDR_PRIx, addr); |
| 1763 | return NULL; |
| 1764 | } |
| 1765 | |
| 1766 | if (!memory_region_is_ram(mrs.mr) && !memory_region_is_romd(mrs.mr)) { |
| 1767 | error_setg(errp, "Memory at address 0x%" HWADDR_PRIx "is not RAM", addr); |
| 1768 | memory_region_unref(mrs.mr); |
| 1769 | return NULL; |
| 1770 | } |
| 1771 | |
| 1772 | *p_mr = mrs.mr; |
| 1773 | return qemu_map_ram_ptr(mrs.mr->ram_block, mrs.offset_within_region); |
| 1774 | } |
| 1775 | |
| 1776 | static void hmp_gpa2hva(Monitor *mon, const QDict *qdict) |
| 1777 | { |
| 1778 | hwaddr addr = qdict_get_int(qdict, "addr"); |
| 1779 | Error *local_err = NULL; |
| 1780 | MemoryRegion *mr = NULL; |
| 1781 | void *ptr; |
| 1782 | |
| 1783 | ptr = gpa2hva(&mr, addr, &local_err); |
| 1784 | if (local_err) { |
| 1785 | error_report_err(local_err); |
| 1786 | return; |
| 1787 | } |
| 1788 | |
| 1789 | monitor_printf(mon, "Host virtual address for 0x%" HWADDR_PRIx |
| 1790 | " (%s) is %p\n", |
| 1791 | addr, mr->name, ptr); |
| 1792 | |
| 1793 | memory_region_unref(mr); |
| 1794 | } |
| 1795 | |
| 1796 | #ifdef CONFIG_LINUX |
| 1797 | static uint64_t vtop(void *ptr, Error **errp) |
| 1798 | { |
| 1799 | uint64_t pinfo; |
| 1800 | uint64_t ret = -1; |
| 1801 | uintptr_t addr = (uintptr_t) ptr; |
| 1802 | uintptr_t pagesize = getpagesize(); |
| 1803 | off_t offset = addr / pagesize * sizeof(pinfo); |
| 1804 | int fd; |
| 1805 | |
| 1806 | fd = open("/proc/self/pagemap", O_RDONLY); |
| 1807 | if (fd == -1) { |
| 1808 | error_setg_errno(errp, errno, "Cannot open /proc/self/pagemap"); |
| 1809 | return -1; |
| 1810 | } |
| 1811 | |
| 1812 | /* Force copy-on-write if necessary. */ |
| 1813 | atomic_add((uint8_t *)ptr, 0); |
| 1814 | |
| 1815 | if (pread(fd, &pinfo, sizeof(pinfo), offset) != sizeof(pinfo)) { |
| 1816 | error_setg_errno(errp, errno, "Cannot read pagemap"); |
| 1817 | goto out; |
| 1818 | } |
| 1819 | if ((pinfo & (1ull << 63)) == 0) { |
| 1820 | error_setg(errp, "Page not present"); |
| 1821 | goto out; |
| 1822 | } |
| 1823 | ret = ((pinfo & 0x007fffffffffffffull) * pagesize) | (addr & (pagesize - 1)); |
| 1824 | |
| 1825 | out: |
| 1826 | close(fd); |
| 1827 | return ret; |
| 1828 | } |
| 1829 | |
| 1830 | static void hmp_gpa2hpa(Monitor *mon, const QDict *qdict) |
| 1831 | { |
| 1832 | hwaddr addr = qdict_get_int(qdict, "addr"); |
| 1833 | Error *local_err = NULL; |
| 1834 | MemoryRegion *mr = NULL; |
| 1835 | void *ptr; |
| 1836 | uint64_t physaddr; |
| 1837 | |
| 1838 | ptr = gpa2hva(&mr, addr, &local_err); |
| 1839 | if (local_err) { |
| 1840 | error_report_err(local_err); |
| 1841 | return; |
| 1842 | } |
| 1843 | |
| 1844 | physaddr = vtop(ptr, &local_err); |
| 1845 | if (local_err) { |
| 1846 | error_report_err(local_err); |
| 1847 | } else { |
| 1848 | monitor_printf(mon, "Host physical address for 0x%" HWADDR_PRIx |
| 1849 | " (%s) is 0x%" PRIx64 "\n", |
| 1850 | addr, mr->name, (uint64_t) physaddr); |
| 1851 | } |
| 1852 | |
| 1853 | memory_region_unref(mr); |
| 1854 | } |
| 1855 | #endif |
| 1856 | |
Luiz Capitulino | 1bd1442 | 2009-08-28 15:27:17 -0300 | [diff] [blame] | 1857 | static void do_print(Monitor *mon, const QDict *qdict) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1858 | { |
Luiz Capitulino | 1bd1442 | 2009-08-28 15:27:17 -0300 | [diff] [blame] | 1859 | int format = qdict_get_int(qdict, "format"); |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1860 | hwaddr val = qdict_get_int(qdict, "val"); |
Luiz Capitulino | 1bd1442 | 2009-08-28 15:27:17 -0300 | [diff] [blame] | 1861 | |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1862 | switch(format) { |
| 1863 | case 'o': |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1864 | monitor_printf(mon, "%#" HWADDR_PRIo, val); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1865 | break; |
| 1866 | case 'x': |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1867 | monitor_printf(mon, "%#" HWADDR_PRIx, val); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1868 | break; |
| 1869 | case 'u': |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1870 | monitor_printf(mon, "%" HWADDR_PRIu, val); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1871 | break; |
| 1872 | default: |
| 1873 | case 'd': |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1874 | monitor_printf(mon, "%" HWADDR_PRId, val); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1875 | break; |
| 1876 | case 'c': |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1877 | monitor_printc(mon, val); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1878 | break; |
| 1879 | } |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1880 | monitor_printf(mon, "\n"); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1881 | } |
| 1882 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 1883 | static void hmp_sum(Monitor *mon, const QDict *qdict) |
bellard | e4cf1ad | 2005-06-04 20:15:57 +0000 | [diff] [blame] | 1884 | { |
| 1885 | uint32_t addr; |
bellard | e4cf1ad | 2005-06-04 20:15:57 +0000 | [diff] [blame] | 1886 | uint16_t sum; |
Luiz Capitulino | f18c16d | 2009-08-28 15:27:14 -0300 | [diff] [blame] | 1887 | uint32_t start = qdict_get_int(qdict, "start"); |
| 1888 | uint32_t size = qdict_get_int(qdict, "size"); |
bellard | e4cf1ad | 2005-06-04 20:15:57 +0000 | [diff] [blame] | 1889 | |
| 1890 | sum = 0; |
| 1891 | for(addr = start; addr < (start + size); addr++) { |
Peter Maydell | 42874d3 | 2015-04-26 16:49:24 +0100 | [diff] [blame] | 1892 | uint8_t val = address_space_ldub(&address_space_memory, addr, |
| 1893 | MEMTXATTRS_UNSPECIFIED, NULL); |
bellard | e4cf1ad | 2005-06-04 20:15:57 +0000 | [diff] [blame] | 1894 | /* BSD sum algorithm ('sum' Unix command) */ |
| 1895 | sum = (sum >> 1) | (sum << 15); |
Stefan Weil | 54f7b4a | 2011-04-10 18:23:39 +0200 | [diff] [blame] | 1896 | sum += val; |
bellard | e4cf1ad | 2005-06-04 20:15:57 +0000 | [diff] [blame] | 1897 | } |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1898 | monitor_printf(mon, "%05d\n", sum); |
bellard | e4cf1ad | 2005-06-04 20:15:57 +0000 | [diff] [blame] | 1899 | } |
| 1900 | |
bellard | 13224a8 | 2006-07-14 22:03:35 +0000 | [diff] [blame] | 1901 | static int mouse_button_state; |
| 1902 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 1903 | static void hmp_mouse_move(Monitor *mon, const QDict *qdict) |
bellard | 13224a8 | 2006-07-14 22:03:35 +0000 | [diff] [blame] | 1904 | { |
Gerd Hoffmann | c751a74 | 2013-12-04 15:02:28 +0100 | [diff] [blame] | 1905 | int dx, dy, dz, button; |
Luiz Capitulino | 1d4daa9 | 2009-08-28 15:27:15 -0300 | [diff] [blame] | 1906 | const char *dx_str = qdict_get_str(qdict, "dx_str"); |
| 1907 | const char *dy_str = qdict_get_str(qdict, "dy_str"); |
| 1908 | const char *dz_str = qdict_get_try_str(qdict, "dz_str"); |
Gerd Hoffmann | c751a74 | 2013-12-04 15:02:28 +0100 | [diff] [blame] | 1909 | |
bellard | 13224a8 | 2006-07-14 22:03:35 +0000 | [diff] [blame] | 1910 | dx = strtol(dx_str, NULL, 0); |
| 1911 | dy = strtol(dy_str, NULL, 0); |
Gerd Hoffmann | c751a74 | 2013-12-04 15:02:28 +0100 | [diff] [blame] | 1912 | qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx); |
| 1913 | qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy); |
| 1914 | |
| 1915 | if (dz_str) { |
bellard | 13224a8 | 2006-07-14 22:03:35 +0000 | [diff] [blame] | 1916 | dz = strtol(dz_str, NULL, 0); |
Gerd Hoffmann | c751a74 | 2013-12-04 15:02:28 +0100 | [diff] [blame] | 1917 | if (dz != 0) { |
Gerd Hoffmann | f22d0af | 2016-01-12 12:14:12 +0100 | [diff] [blame] | 1918 | button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN; |
Gerd Hoffmann | c751a74 | 2013-12-04 15:02:28 +0100 | [diff] [blame] | 1919 | qemu_input_queue_btn(NULL, button, true); |
| 1920 | qemu_input_event_sync(); |
| 1921 | qemu_input_queue_btn(NULL, button, false); |
| 1922 | } |
| 1923 | } |
| 1924 | qemu_input_event_sync(); |
bellard | 13224a8 | 2006-07-14 22:03:35 +0000 | [diff] [blame] | 1925 | } |
| 1926 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 1927 | static void hmp_mouse_button(Monitor *mon, const QDict *qdict) |
bellard | 13224a8 | 2006-07-14 22:03:35 +0000 | [diff] [blame] | 1928 | { |
Eric Blake | 7fb1cf1 | 2015-11-18 01:52:57 -0700 | [diff] [blame] | 1929 | static uint32_t bmap[INPUT_BUTTON__MAX] = { |
Gerd Hoffmann | c751a74 | 2013-12-04 15:02:28 +0100 | [diff] [blame] | 1930 | [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON, |
| 1931 | [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON, |
| 1932 | [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON, |
| 1933 | }; |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 1934 | int button_state = qdict_get_int(qdict, "button_state"); |
Gerd Hoffmann | c751a74 | 2013-12-04 15:02:28 +0100 | [diff] [blame] | 1935 | |
| 1936 | if (mouse_button_state == button_state) { |
| 1937 | return; |
| 1938 | } |
| 1939 | qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state); |
| 1940 | qemu_input_event_sync(); |
bellard | 13224a8 | 2006-07-14 22:03:35 +0000 | [diff] [blame] | 1941 | mouse_button_state = button_state; |
bellard | 13224a8 | 2006-07-14 22:03:35 +0000 | [diff] [blame] | 1942 | } |
| 1943 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 1944 | static void hmp_ioport_read(Monitor *mon, const QDict *qdict) |
bellard | 3440557 | 2004-06-08 00:55:58 +0000 | [diff] [blame] | 1945 | { |
Luiz Capitulino | aa93e39 | 2009-08-28 15:27:18 -0300 | [diff] [blame] | 1946 | int size = qdict_get_int(qdict, "size"); |
| 1947 | int addr = qdict_get_int(qdict, "addr"); |
| 1948 | int has_index = qdict_haskey(qdict, "index"); |
bellard | 3440557 | 2004-06-08 00:55:58 +0000 | [diff] [blame] | 1949 | uint32_t val; |
| 1950 | int suffix; |
| 1951 | |
| 1952 | if (has_index) { |
Luiz Capitulino | aa93e39 | 2009-08-28 15:27:18 -0300 | [diff] [blame] | 1953 | int index = qdict_get_int(qdict, "index"); |
Blue Swirl | afcea8c | 2009-09-20 16:05:47 +0000 | [diff] [blame] | 1954 | cpu_outb(addr & IOPORTS_MASK, index & 0xff); |
bellard | 3440557 | 2004-06-08 00:55:58 +0000 | [diff] [blame] | 1955 | addr++; |
| 1956 | } |
| 1957 | addr &= 0xffff; |
| 1958 | |
| 1959 | switch(size) { |
| 1960 | default: |
| 1961 | case 1: |
Blue Swirl | afcea8c | 2009-09-20 16:05:47 +0000 | [diff] [blame] | 1962 | val = cpu_inb(addr); |
bellard | 3440557 | 2004-06-08 00:55:58 +0000 | [diff] [blame] | 1963 | suffix = 'b'; |
| 1964 | break; |
| 1965 | case 2: |
Blue Swirl | afcea8c | 2009-09-20 16:05:47 +0000 | [diff] [blame] | 1966 | val = cpu_inw(addr); |
bellard | 3440557 | 2004-06-08 00:55:58 +0000 | [diff] [blame] | 1967 | suffix = 'w'; |
| 1968 | break; |
| 1969 | case 4: |
Blue Swirl | afcea8c | 2009-09-20 16:05:47 +0000 | [diff] [blame] | 1970 | val = cpu_inl(addr); |
bellard | 3440557 | 2004-06-08 00:55:58 +0000 | [diff] [blame] | 1971 | suffix = 'l'; |
| 1972 | break; |
| 1973 | } |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1974 | monitor_printf(mon, "port%c[0x%04x] = %#0*x\n", |
| 1975 | suffix, addr, size * 2, val); |
bellard | 3440557 | 2004-06-08 00:55:58 +0000 | [diff] [blame] | 1976 | } |
bellard | a3a91a3 | 2004-06-04 11:06:21 +0000 | [diff] [blame] | 1977 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 1978 | static void hmp_ioport_write(Monitor *mon, const QDict *qdict) |
Jan Kiszka | f114784 | 2009-07-14 10:20:11 +0200 | [diff] [blame] | 1979 | { |
Luiz Capitulino | 1bd1442 | 2009-08-28 15:27:17 -0300 | [diff] [blame] | 1980 | int size = qdict_get_int(qdict, "size"); |
| 1981 | int addr = qdict_get_int(qdict, "addr"); |
| 1982 | int val = qdict_get_int(qdict, "val"); |
| 1983 | |
Jan Kiszka | f114784 | 2009-07-14 10:20:11 +0200 | [diff] [blame] | 1984 | addr &= IOPORTS_MASK; |
| 1985 | |
| 1986 | switch (size) { |
| 1987 | default: |
| 1988 | case 1: |
Blue Swirl | afcea8c | 2009-09-20 16:05:47 +0000 | [diff] [blame] | 1989 | cpu_outb(addr, val); |
Jan Kiszka | f114784 | 2009-07-14 10:20:11 +0200 | [diff] [blame] | 1990 | break; |
| 1991 | case 2: |
Blue Swirl | afcea8c | 2009-09-20 16:05:47 +0000 | [diff] [blame] | 1992 | cpu_outw(addr, val); |
Jan Kiszka | f114784 | 2009-07-14 10:20:11 +0200 | [diff] [blame] | 1993 | break; |
| 1994 | case 4: |
Blue Swirl | afcea8c | 2009-09-20 16:05:47 +0000 | [diff] [blame] | 1995 | cpu_outl(addr, val); |
Jan Kiszka | f114784 | 2009-07-14 10:20:11 +0200 | [diff] [blame] | 1996 | break; |
| 1997 | } |
| 1998 | } |
| 1999 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 2000 | static void hmp_boot_set(Monitor *mon, const QDict *qdict) |
aurel32 | 0ecdffb | 2008-05-04 20:11:34 +0000 | [diff] [blame] | 2001 | { |
Gonglei | f183993 | 2014-12-03 18:20:58 +0000 | [diff] [blame] | 2002 | Error *local_err = NULL; |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 2003 | const char *bootdevice = qdict_get_str(qdict, "bootdevice"); |
aurel32 | 0ecdffb | 2008-05-04 20:11:34 +0000 | [diff] [blame] | 2004 | |
Gonglei | f183993 | 2014-12-03 18:20:58 +0000 | [diff] [blame] | 2005 | qemu_boot_set(bootdevice, &local_err); |
| 2006 | if (local_err) { |
Markus Armbruster | 193227f | 2015-12-18 16:35:06 +0100 | [diff] [blame] | 2007 | error_report_err(local_err); |
aurel32 | 0ecdffb | 2008-05-04 20:11:34 +0000 | [diff] [blame] | 2008 | } else { |
Gonglei | f183993 | 2014-12-03 18:20:58 +0000 | [diff] [blame] | 2009 | monitor_printf(mon, "boot device list now set to %s\n", bootdevice); |
aurel32 | 0ecdffb | 2008-05-04 20:11:34 +0000 | [diff] [blame] | 2010 | } |
| 2011 | } |
| 2012 | |
Markus Armbruster | 1ce6be2 | 2015-02-06 14:18:24 +0100 | [diff] [blame] | 2013 | static void hmp_info_mtree(Monitor *mon, const QDict *qdict) |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 2014 | { |
Peter Xu | 57bb40c | 2017-01-16 16:40:05 +0800 | [diff] [blame] | 2015 | bool flatview = qdict_get_try_bool(qdict, "flatview", false); |
Alexey Kardashevskiy | 5e8fd94 | 2017-09-21 18:51:06 +1000 | [diff] [blame] | 2016 | bool dispatch_tree = qdict_get_try_bool(qdict, "dispatch_tree", false); |
Alexey Kardashevskiy | fc051ae | 2018-06-04 13:25:11 +1000 | [diff] [blame] | 2017 | bool owner = qdict_get_try_bool(qdict, "owner", false); |
Peter Xu | 57bb40c | 2017-01-16 16:40:05 +0800 | [diff] [blame] | 2018 | |
Alexey Kardashevskiy | fc051ae | 2018-06-04 13:25:11 +1000 | [diff] [blame] | 2019 | mtree_info((fprintf_function)monitor_printf, mon, flatview, dispatch_tree, |
| 2020 | owner); |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 2021 | } |
| 2022 | |
Markus Armbruster | 1ce6be2 | 2015-02-06 14:18:24 +0100 | [diff] [blame] | 2023 | static void hmp_info_numa(Monitor *mon, const QDict *qdict) |
aliguori | 030ea37 | 2009-04-21 22:30:47 +0000 | [diff] [blame] | 2024 | { |
aliguori | b28b623 | 2009-04-22 20:20:29 +0000 | [diff] [blame] | 2025 | int i; |
Vadim Galitsyn | 31959e8 | 2017-08-29 17:30:20 +0200 | [diff] [blame] | 2026 | NumaNodeMem *node_mem; |
Igor Mammedov | f75cd44 | 2017-05-30 18:23:59 +0200 | [diff] [blame] | 2027 | CpuInfoList *cpu_list, *cpu; |
aliguori | 030ea37 | 2009-04-21 22:30:47 +0000 | [diff] [blame] | 2028 | |
Igor Mammedov | f75cd44 | 2017-05-30 18:23:59 +0200 | [diff] [blame] | 2029 | cpu_list = qmp_query_cpus(&error_abort); |
Vadim Galitsyn | 31959e8 | 2017-08-29 17:30:20 +0200 | [diff] [blame] | 2030 | node_mem = g_new0(NumaNodeMem, nb_numa_nodes); |
| 2031 | |
zhanghailiang | 5b009e4 | 2014-11-04 19:49:30 +0800 | [diff] [blame] | 2032 | query_numa_node_mem(node_mem); |
aliguori | 030ea37 | 2009-04-21 22:30:47 +0000 | [diff] [blame] | 2033 | monitor_printf(mon, "%d nodes\n", nb_numa_nodes); |
| 2034 | for (i = 0; i < nb_numa_nodes; i++) { |
| 2035 | monitor_printf(mon, "node %d cpus:", i); |
Igor Mammedov | f75cd44 | 2017-05-30 18:23:59 +0200 | [diff] [blame] | 2036 | for (cpu = cpu_list; cpu; cpu = cpu->next) { |
| 2037 | if (cpu->value->has_props && cpu->value->props->has_node_id && |
| 2038 | cpu->value->props->node_id == i) { |
| 2039 | monitor_printf(mon, " %" PRIi64, cpu->value->CPU); |
aliguori | 030ea37 | 2009-04-21 22:30:47 +0000 | [diff] [blame] | 2040 | } |
| 2041 | } |
| 2042 | monitor_printf(mon, "\n"); |
| 2043 | monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i, |
Vadim Galitsyn | 31959e8 | 2017-08-29 17:30:20 +0200 | [diff] [blame] | 2044 | node_mem[i].node_mem >> 20); |
| 2045 | monitor_printf(mon, "node %d plugged: %" PRId64 " MB\n", i, |
| 2046 | node_mem[i].node_plugged_mem >> 20); |
aliguori | 030ea37 | 2009-04-21 22:30:47 +0000 | [diff] [blame] | 2047 | } |
Igor Mammedov | f75cd44 | 2017-05-30 18:23:59 +0200 | [diff] [blame] | 2048 | qapi_free_CpuInfoList(cpu_list); |
zhanghailiang | 5b009e4 | 2014-11-04 19:49:30 +0800 | [diff] [blame] | 2049 | g_free(node_mem); |
aliguori | 030ea37 | 2009-04-21 22:30:47 +0000 | [diff] [blame] | 2050 | } |
| 2051 | |
bellard | 5f1ce94 | 2006-02-08 22:40:15 +0000 | [diff] [blame] | 2052 | #ifdef CONFIG_PROFILER |
| 2053 | |
Alexey Kardashevskiy | 89d5cbd | 2015-03-16 14:57:38 +1100 | [diff] [blame] | 2054 | int64_t tcg_time; |
Aurelien Jarno | e9a6625 | 2009-09-30 14:09:52 +0200 | [diff] [blame] | 2055 | int64_t dev_time; |
| 2056 | |
Markus Armbruster | 1ce6be2 | 2015-02-06 14:18:24 +0100 | [diff] [blame] | 2057 | static void hmp_info_profile(Monitor *mon, const QDict *qdict) |
bellard | 5f1ce94 | 2006-02-08 22:40:15 +0000 | [diff] [blame] | 2058 | { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2059 | monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n", |
Rutuja Shah | 73bcb24 | 2016-03-21 21:32:30 +0530 | [diff] [blame] | 2060 | dev_time, dev_time / (double)NANOSECONDS_PER_SECOND); |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2061 | monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n", |
Rutuja Shah | 73bcb24 | 2016-03-21 21:32:30 +0530 | [diff] [blame] | 2062 | tcg_time, tcg_time / (double)NANOSECONDS_PER_SECOND); |
Alexey Kardashevskiy | 89d5cbd | 2015-03-16 14:57:38 +1100 | [diff] [blame] | 2063 | tcg_time = 0; |
bellard | 5f1ce94 | 2006-02-08 22:40:15 +0000 | [diff] [blame] | 2064 | dev_time = 0; |
bellard | 5f1ce94 | 2006-02-08 22:40:15 +0000 | [diff] [blame] | 2065 | } |
| 2066 | #else |
Markus Armbruster | 1ce6be2 | 2015-02-06 14:18:24 +0100 | [diff] [blame] | 2067 | static void hmp_info_profile(Monitor *mon, const QDict *qdict) |
bellard | 5f1ce94 | 2006-02-08 22:40:15 +0000 | [diff] [blame] | 2068 | { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2069 | monitor_printf(mon, "Internal profiler not compiled\n"); |
bellard | 5f1ce94 | 2006-02-08 22:40:15 +0000 | [diff] [blame] | 2070 | } |
| 2071 | #endif |
| 2072 | |
bellard | ec36b69 | 2006-07-16 18:57:03 +0000 | [diff] [blame] | 2073 | /* Capture support */ |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 2074 | static QLIST_HEAD (capture_list_head, CaptureState) capture_head; |
bellard | ec36b69 | 2006-07-16 18:57:03 +0000 | [diff] [blame] | 2075 | |
Markus Armbruster | 1ce6be2 | 2015-02-06 14:18:24 +0100 | [diff] [blame] | 2076 | static void hmp_info_capture(Monitor *mon, const QDict *qdict) |
bellard | ec36b69 | 2006-07-16 18:57:03 +0000 | [diff] [blame] | 2077 | { |
| 2078 | int i; |
| 2079 | CaptureState *s; |
| 2080 | |
| 2081 | for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2082 | monitor_printf(mon, "[%d]: ", i); |
bellard | ec36b69 | 2006-07-16 18:57:03 +0000 | [diff] [blame] | 2083 | s->ops.info (s->opaque); |
| 2084 | } |
| 2085 | } |
| 2086 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 2087 | static void hmp_stopcapture(Monitor *mon, const QDict *qdict) |
bellard | ec36b69 | 2006-07-16 18:57:03 +0000 | [diff] [blame] | 2088 | { |
| 2089 | int i; |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 2090 | int n = qdict_get_int(qdict, "n"); |
bellard | ec36b69 | 2006-07-16 18:57:03 +0000 | [diff] [blame] | 2091 | CaptureState *s; |
| 2092 | |
| 2093 | for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) { |
| 2094 | if (i == n) { |
| 2095 | s->ops.destroy (s->opaque); |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 2096 | QLIST_REMOVE (s, entries); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2097 | g_free (s); |
bellard | ec36b69 | 2006-07-16 18:57:03 +0000 | [diff] [blame] | 2098 | return; |
| 2099 | } |
| 2100 | } |
| 2101 | } |
| 2102 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 2103 | static void hmp_wavcapture(Monitor *mon, const QDict *qdict) |
bellard | ec36b69 | 2006-07-16 18:57:03 +0000 | [diff] [blame] | 2104 | { |
Luiz Capitulino | c192548 | 2009-08-28 15:27:19 -0300 | [diff] [blame] | 2105 | const char *path = qdict_get_str(qdict, "path"); |
| 2106 | int has_freq = qdict_haskey(qdict, "freq"); |
| 2107 | int freq = qdict_get_try_int(qdict, "freq", -1); |
| 2108 | int has_bits = qdict_haskey(qdict, "bits"); |
| 2109 | int bits = qdict_get_try_int(qdict, "bits", -1); |
| 2110 | int has_channels = qdict_haskey(qdict, "nchannels"); |
| 2111 | int nchannels = qdict_get_try_int(qdict, "nchannels", -1); |
bellard | ec36b69 | 2006-07-16 18:57:03 +0000 | [diff] [blame] | 2112 | CaptureState *s; |
| 2113 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2114 | s = g_malloc0 (sizeof (*s)); |
bellard | ec36b69 | 2006-07-16 18:57:03 +0000 | [diff] [blame] | 2115 | |
| 2116 | freq = has_freq ? freq : 44100; |
| 2117 | bits = has_bits ? bits : 16; |
| 2118 | nchannels = has_channels ? nchannels : 2; |
| 2119 | |
| 2120 | if (wav_start_capture (s, path, freq, bits, nchannels)) { |
Isaku Yamahata | d00b261 | 2011-01-21 19:53:55 +0900 | [diff] [blame] | 2121 | monitor_printf(mon, "Failed to add wave capture\n"); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2122 | g_free (s); |
Isaku Yamahata | d00b261 | 2011-01-21 19:53:55 +0900 | [diff] [blame] | 2123 | return; |
bellard | ec36b69 | 2006-07-16 18:57:03 +0000 | [diff] [blame] | 2124 | } |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 2125 | QLIST_INSERT_HEAD (&capture_head, s, entries); |
bellard | ec36b69 | 2006-07-16 18:57:03 +0000 | [diff] [blame] | 2126 | } |
bellard | ec36b69 | 2006-07-16 18:57:03 +0000 | [diff] [blame] | 2127 | |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 2128 | static qemu_acl *find_acl(Monitor *mon, const char *name) |
aliguori | 76655d6 | 2009-03-06 20:27:37 +0000 | [diff] [blame] | 2129 | { |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 2130 | qemu_acl *acl = qemu_acl_find(name); |
aliguori | 76655d6 | 2009-03-06 20:27:37 +0000 | [diff] [blame] | 2131 | |
aliguori | 76655d6 | 2009-03-06 20:27:37 +0000 | [diff] [blame] | 2132 | if (!acl) { |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 2133 | monitor_printf(mon, "acl: unknown list '%s'\n", name); |
aliguori | 76655d6 | 2009-03-06 20:27:37 +0000 | [diff] [blame] | 2134 | } |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 2135 | return acl; |
| 2136 | } |
aliguori | 76655d6 | 2009-03-06 20:27:37 +0000 | [diff] [blame] | 2137 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 2138 | static void hmp_acl_show(Monitor *mon, const QDict *qdict) |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 2139 | { |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 2140 | const char *aclname = qdict_get_str(qdict, "aclname"); |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 2141 | qemu_acl *acl = find_acl(mon, aclname); |
| 2142 | qemu_acl_entry *entry; |
| 2143 | int i = 0; |
| 2144 | |
| 2145 | if (acl) { |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 2146 | monitor_printf(mon, "policy: %s\n", |
aliguori | 76655d6 | 2009-03-06 20:27:37 +0000 | [diff] [blame] | 2147 | acl->defaultDeny ? "deny" : "allow"); |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 2148 | QTAILQ_FOREACH(entry, &acl->entries, next) { |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 2149 | i++; |
| 2150 | monitor_printf(mon, "%d: %s %s\n", i, |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 2151 | entry->deny ? "deny" : "allow", entry->match); |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 2152 | } |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 2153 | } |
| 2154 | } |
| 2155 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 2156 | static void hmp_acl_reset(Monitor *mon, const QDict *qdict) |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 2157 | { |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 2158 | const char *aclname = qdict_get_str(qdict, "aclname"); |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 2159 | qemu_acl *acl = find_acl(mon, aclname); |
| 2160 | |
| 2161 | if (acl) { |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 2162 | qemu_acl_reset(acl); |
| 2163 | monitor_printf(mon, "acl: removed all rules\n"); |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 2164 | } |
| 2165 | } |
aliguori | 76655d6 | 2009-03-06 20:27:37 +0000 | [diff] [blame] | 2166 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 2167 | static void hmp_acl_policy(Monitor *mon, const QDict *qdict) |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 2168 | { |
Luiz Capitulino | f18c16d | 2009-08-28 15:27:14 -0300 | [diff] [blame] | 2169 | const char *aclname = qdict_get_str(qdict, "aclname"); |
| 2170 | const char *policy = qdict_get_str(qdict, "policy"); |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 2171 | qemu_acl *acl = find_acl(mon, aclname); |
| 2172 | |
| 2173 | if (acl) { |
| 2174 | if (strcmp(policy, "allow") == 0) { |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 2175 | acl->defaultDeny = 0; |
| 2176 | monitor_printf(mon, "acl: policy set to 'allow'\n"); |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 2177 | } else if (strcmp(policy, "deny") == 0) { |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 2178 | acl->defaultDeny = 1; |
| 2179 | monitor_printf(mon, "acl: policy set to 'deny'\n"); |
| 2180 | } else { |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 2181 | monitor_printf(mon, "acl: unknown policy '%s', " |
| 2182 | "expected 'deny' or 'allow'\n", policy); |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 2183 | } |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 2184 | } |
| 2185 | } |
aliguori | 76655d6 | 2009-03-06 20:27:37 +0000 | [diff] [blame] | 2186 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 2187 | static void hmp_acl_add(Monitor *mon, const QDict *qdict) |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 2188 | { |
Luiz Capitulino | 1bd1442 | 2009-08-28 15:27:17 -0300 | [diff] [blame] | 2189 | const char *aclname = qdict_get_str(qdict, "aclname"); |
| 2190 | const char *match = qdict_get_str(qdict, "match"); |
| 2191 | const char *policy = qdict_get_str(qdict, "policy"); |
| 2192 | int has_index = qdict_haskey(qdict, "index"); |
| 2193 | int index = qdict_get_try_int(qdict, "index", -1); |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 2194 | qemu_acl *acl = find_acl(mon, aclname); |
| 2195 | int deny, ret; |
| 2196 | |
| 2197 | if (acl) { |
| 2198 | if (strcmp(policy, "allow") == 0) { |
| 2199 | deny = 0; |
| 2200 | } else if (strcmp(policy, "deny") == 0) { |
| 2201 | deny = 1; |
| 2202 | } else { |
| 2203 | monitor_printf(mon, "acl: unknown policy '%s', " |
| 2204 | "expected 'deny' or 'allow'\n", policy); |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 2205 | return; |
| 2206 | } |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 2207 | if (has_index) |
| 2208 | ret = qemu_acl_insert(acl, deny, match, index); |
| 2209 | else |
| 2210 | ret = qemu_acl_append(acl, deny, match); |
| 2211 | if (ret < 0) |
| 2212 | monitor_printf(mon, "acl: unable to add acl entry\n"); |
| 2213 | else |
| 2214 | monitor_printf(mon, "acl: added rule at position %d\n", ret); |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 2215 | } |
| 2216 | } |
aliguori | 76655d6 | 2009-03-06 20:27:37 +0000 | [diff] [blame] | 2217 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 2218 | static void hmp_acl_remove(Monitor *mon, const QDict *qdict) |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 2219 | { |
Luiz Capitulino | f18c16d | 2009-08-28 15:27:14 -0300 | [diff] [blame] | 2220 | const char *aclname = qdict_get_str(qdict, "aclname"); |
| 2221 | const char *match = qdict_get_str(qdict, "match"); |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 2222 | qemu_acl *acl = find_acl(mon, aclname); |
| 2223 | int ret; |
aliguori | 76655d6 | 2009-03-06 20:27:37 +0000 | [diff] [blame] | 2224 | |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 2225 | if (acl) { |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 2226 | ret = qemu_acl_remove(acl, match); |
| 2227 | if (ret < 0) |
| 2228 | monitor_printf(mon, "acl: no matching acl entry\n"); |
| 2229 | else |
| 2230 | monitor_printf(mon, "acl: removed rule at position %d\n", ret); |
aliguori | 76655d6 | 2009-03-06 20:27:37 +0000 | [diff] [blame] | 2231 | } |
| 2232 | } |
| 2233 | |
Corey Bryant | 208c9d1 | 2012-06-22 14:36:09 -0400 | [diff] [blame] | 2234 | void qmp_getfd(const char *fdname, Error **errp) |
Mark McLoughlin | f07918f | 2009-07-22 09:11:40 +0100 | [diff] [blame] | 2235 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2236 | mon_fd_t *monfd; |
Peter Xu | 9409fc0 | 2018-06-08 11:55:06 +0800 | [diff] [blame] | 2237 | int fd, tmp_fd; |
Mark McLoughlin | f07918f | 2009-07-22 09:11:40 +0100 | [diff] [blame] | 2238 | |
Marc-André Lureau | 5345fdb | 2016-10-22 12:52:55 +0300 | [diff] [blame] | 2239 | fd = qemu_chr_fe_get_msgfd(&cur_mon->chr); |
Mark McLoughlin | f07918f | 2009-07-22 09:11:40 +0100 | [diff] [blame] | 2240 | if (fd == -1) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 2241 | error_setg(errp, QERR_FD_NOT_SUPPLIED); |
Corey Bryant | 208c9d1 | 2012-06-22 14:36:09 -0400 | [diff] [blame] | 2242 | return; |
Mark McLoughlin | f07918f | 2009-07-22 09:11:40 +0100 | [diff] [blame] | 2243 | } |
| 2244 | |
| 2245 | if (qemu_isdigit(fdname[0])) { |
Stefan Hajnoczi | 0b9f0e2 | 2014-04-24 13:58:18 +0200 | [diff] [blame] | 2246 | close(fd); |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 2247 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname", |
| 2248 | "a name not starting with a digit"); |
Corey Bryant | 208c9d1 | 2012-06-22 14:36:09 -0400 | [diff] [blame] | 2249 | return; |
Mark McLoughlin | f07918f | 2009-07-22 09:11:40 +0100 | [diff] [blame] | 2250 | } |
| 2251 | |
Peter Xu | 9409fc0 | 2018-06-08 11:55:06 +0800 | [diff] [blame] | 2252 | qemu_mutex_lock(&cur_mon->mon_lock); |
Corey Bryant | 208c9d1 | 2012-06-22 14:36:09 -0400 | [diff] [blame] | 2253 | QLIST_FOREACH(monfd, &cur_mon->fds, next) { |
Mark McLoughlin | f07918f | 2009-07-22 09:11:40 +0100 | [diff] [blame] | 2254 | if (strcmp(monfd->name, fdname) != 0) { |
| 2255 | continue; |
| 2256 | } |
| 2257 | |
Peter Xu | 9409fc0 | 2018-06-08 11:55:06 +0800 | [diff] [blame] | 2258 | tmp_fd = monfd->fd; |
Mark McLoughlin | f07918f | 2009-07-22 09:11:40 +0100 | [diff] [blame] | 2259 | monfd->fd = fd; |
Peter Xu | 9409fc0 | 2018-06-08 11:55:06 +0800 | [diff] [blame] | 2260 | qemu_mutex_unlock(&cur_mon->mon_lock); |
Markus Armbruster | 774a6b6 | 2018-07-03 10:53:57 +0200 | [diff] [blame] | 2261 | /* Make sure close() is outside critical section */ |
Peter Xu | 9409fc0 | 2018-06-08 11:55:06 +0800 | [diff] [blame] | 2262 | close(tmp_fd); |
Corey Bryant | 208c9d1 | 2012-06-22 14:36:09 -0400 | [diff] [blame] | 2263 | return; |
Mark McLoughlin | f07918f | 2009-07-22 09:11:40 +0100 | [diff] [blame] | 2264 | } |
| 2265 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2266 | monfd = g_malloc0(sizeof(mon_fd_t)); |
| 2267 | monfd->name = g_strdup(fdname); |
Mark McLoughlin | f07918f | 2009-07-22 09:11:40 +0100 | [diff] [blame] | 2268 | monfd->fd = fd; |
| 2269 | |
Corey Bryant | 208c9d1 | 2012-06-22 14:36:09 -0400 | [diff] [blame] | 2270 | QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next); |
Peter Xu | 9409fc0 | 2018-06-08 11:55:06 +0800 | [diff] [blame] | 2271 | qemu_mutex_unlock(&cur_mon->mon_lock); |
Mark McLoughlin | f07918f | 2009-07-22 09:11:40 +0100 | [diff] [blame] | 2272 | } |
| 2273 | |
Corey Bryant | 208c9d1 | 2012-06-22 14:36:09 -0400 | [diff] [blame] | 2274 | void qmp_closefd(const char *fdname, Error **errp) |
Mark McLoughlin | f07918f | 2009-07-22 09:11:40 +0100 | [diff] [blame] | 2275 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2276 | mon_fd_t *monfd; |
Peter Xu | 9409fc0 | 2018-06-08 11:55:06 +0800 | [diff] [blame] | 2277 | int tmp_fd; |
Mark McLoughlin | f07918f | 2009-07-22 09:11:40 +0100 | [diff] [blame] | 2278 | |
Peter Xu | 9409fc0 | 2018-06-08 11:55:06 +0800 | [diff] [blame] | 2279 | qemu_mutex_lock(&cur_mon->mon_lock); |
Corey Bryant | 208c9d1 | 2012-06-22 14:36:09 -0400 | [diff] [blame] | 2280 | QLIST_FOREACH(monfd, &cur_mon->fds, next) { |
Mark McLoughlin | f07918f | 2009-07-22 09:11:40 +0100 | [diff] [blame] | 2281 | if (strcmp(monfd->name, fdname) != 0) { |
| 2282 | continue; |
| 2283 | } |
| 2284 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 2285 | QLIST_REMOVE(monfd, next); |
Peter Xu | 9409fc0 | 2018-06-08 11:55:06 +0800 | [diff] [blame] | 2286 | tmp_fd = monfd->fd; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2287 | g_free(monfd->name); |
| 2288 | g_free(monfd); |
Peter Xu | 9409fc0 | 2018-06-08 11:55:06 +0800 | [diff] [blame] | 2289 | qemu_mutex_unlock(&cur_mon->mon_lock); |
Markus Armbruster | 774a6b6 | 2018-07-03 10:53:57 +0200 | [diff] [blame] | 2290 | /* Make sure close() is outside critical section */ |
Peter Xu | 9409fc0 | 2018-06-08 11:55:06 +0800 | [diff] [blame] | 2291 | close(tmp_fd); |
Corey Bryant | 208c9d1 | 2012-06-22 14:36:09 -0400 | [diff] [blame] | 2292 | return; |
Mark McLoughlin | f07918f | 2009-07-22 09:11:40 +0100 | [diff] [blame] | 2293 | } |
| 2294 | |
Peter Xu | 9409fc0 | 2018-06-08 11:55:06 +0800 | [diff] [blame] | 2295 | qemu_mutex_unlock(&cur_mon->mon_lock); |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 2296 | error_setg(errp, QERR_FD_NOT_FOUND, fdname); |
Mark McLoughlin | f07918f | 2009-07-22 09:11:40 +0100 | [diff] [blame] | 2297 | } |
| 2298 | |
Paolo Bonzini | a9940fc | 2012-09-20 16:50:32 +0200 | [diff] [blame] | 2299 | int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp) |
Mark McLoughlin | 7768e04 | 2009-07-22 09:11:41 +0100 | [diff] [blame] | 2300 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2301 | mon_fd_t *monfd; |
Mark McLoughlin | 7768e04 | 2009-07-22 09:11:41 +0100 | [diff] [blame] | 2302 | |
Peter Xu | 9409fc0 | 2018-06-08 11:55:06 +0800 | [diff] [blame] | 2303 | qemu_mutex_lock(&mon->mon_lock); |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 2304 | QLIST_FOREACH(monfd, &mon->fds, next) { |
Mark McLoughlin | 7768e04 | 2009-07-22 09:11:41 +0100 | [diff] [blame] | 2305 | int fd; |
| 2306 | |
| 2307 | if (strcmp(monfd->name, fdname) != 0) { |
| 2308 | continue; |
| 2309 | } |
| 2310 | |
| 2311 | fd = monfd->fd; |
| 2312 | |
| 2313 | /* caller takes ownership of fd */ |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 2314 | QLIST_REMOVE(monfd, next); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2315 | g_free(monfd->name); |
| 2316 | g_free(monfd); |
Peter Xu | 9409fc0 | 2018-06-08 11:55:06 +0800 | [diff] [blame] | 2317 | qemu_mutex_unlock(&mon->mon_lock); |
Mark McLoughlin | 7768e04 | 2009-07-22 09:11:41 +0100 | [diff] [blame] | 2318 | |
| 2319 | return fd; |
| 2320 | } |
| 2321 | |
Peter Xu | 9409fc0 | 2018-06-08 11:55:06 +0800 | [diff] [blame] | 2322 | qemu_mutex_unlock(&mon->mon_lock); |
Paolo Bonzini | a9940fc | 2012-09-20 16:50:32 +0200 | [diff] [blame] | 2323 | error_setg(errp, "File descriptor named '%s' has not been found", fdname); |
Mark McLoughlin | 7768e04 | 2009-07-22 09:11:41 +0100 | [diff] [blame] | 2324 | return -1; |
| 2325 | } |
| 2326 | |
Corey Bryant | ba1c048 | 2012-08-14 16:43:43 -0400 | [diff] [blame] | 2327 | static void monitor_fdset_cleanup(MonFdset *mon_fdset) |
| 2328 | { |
| 2329 | MonFdsetFd *mon_fdset_fd; |
| 2330 | MonFdsetFd *mon_fdset_fd_next; |
| 2331 | |
| 2332 | QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) { |
Corey Bryant | ebe52b5 | 2012-10-18 15:19:33 -0400 | [diff] [blame] | 2333 | if ((mon_fdset_fd->removed || |
| 2334 | (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) && |
| 2335 | runstate_is_running()) { |
Corey Bryant | ba1c048 | 2012-08-14 16:43:43 -0400 | [diff] [blame] | 2336 | close(mon_fdset_fd->fd); |
| 2337 | g_free(mon_fdset_fd->opaque); |
| 2338 | QLIST_REMOVE(mon_fdset_fd, next); |
| 2339 | g_free(mon_fdset_fd); |
| 2340 | } |
| 2341 | } |
| 2342 | |
Corey Bryant | adb696f | 2012-08-14 16:43:47 -0400 | [diff] [blame] | 2343 | if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) { |
Corey Bryant | ba1c048 | 2012-08-14 16:43:43 -0400 | [diff] [blame] | 2344 | QLIST_REMOVE(mon_fdset, next); |
| 2345 | g_free(mon_fdset); |
| 2346 | } |
| 2347 | } |
| 2348 | |
Corey Bryant | efb87c1 | 2012-08-14 16:43:48 -0400 | [diff] [blame] | 2349 | static void monitor_fdsets_cleanup(void) |
| 2350 | { |
| 2351 | MonFdset *mon_fdset; |
| 2352 | MonFdset *mon_fdset_next; |
| 2353 | |
Peter Xu | 4745146 | 2018-06-08 11:55:11 +0800 | [diff] [blame] | 2354 | qemu_mutex_lock(&mon_fdsets_lock); |
Corey Bryant | efb87c1 | 2012-08-14 16:43:48 -0400 | [diff] [blame] | 2355 | QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) { |
| 2356 | monitor_fdset_cleanup(mon_fdset); |
| 2357 | } |
Peter Xu | 4745146 | 2018-06-08 11:55:11 +0800 | [diff] [blame] | 2358 | qemu_mutex_unlock(&mon_fdsets_lock); |
Corey Bryant | efb87c1 | 2012-08-14 16:43:48 -0400 | [diff] [blame] | 2359 | } |
| 2360 | |
Corey Bryant | ba1c048 | 2012-08-14 16:43:43 -0400 | [diff] [blame] | 2361 | AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque, |
| 2362 | const char *opaque, Error **errp) |
| 2363 | { |
| 2364 | int fd; |
| 2365 | Monitor *mon = cur_mon; |
Corey Bryant | ba1c048 | 2012-08-14 16:43:43 -0400 | [diff] [blame] | 2366 | AddfdInfo *fdinfo; |
| 2367 | |
Marc-André Lureau | 5345fdb | 2016-10-22 12:52:55 +0300 | [diff] [blame] | 2368 | fd = qemu_chr_fe_get_msgfd(&mon->chr); |
Corey Bryant | ba1c048 | 2012-08-14 16:43:43 -0400 | [diff] [blame] | 2369 | if (fd == -1) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 2370 | error_setg(errp, QERR_FD_NOT_SUPPLIED); |
Corey Bryant | ba1c048 | 2012-08-14 16:43:43 -0400 | [diff] [blame] | 2371 | goto error; |
| 2372 | } |
| 2373 | |
Corey Bryant | e446f70 | 2012-10-18 15:19:32 -0400 | [diff] [blame] | 2374 | fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id, |
| 2375 | has_opaque, opaque, errp); |
| 2376 | if (fdinfo) { |
| 2377 | return fdinfo; |
Corey Bryant | 9ac54af | 2012-10-18 15:19:31 -0400 | [diff] [blame] | 2378 | } |
| 2379 | |
Corey Bryant | ba1c048 | 2012-08-14 16:43:43 -0400 | [diff] [blame] | 2380 | error: |
| 2381 | if (fd != -1) { |
| 2382 | close(fd); |
| 2383 | } |
| 2384 | return NULL; |
| 2385 | } |
| 2386 | |
| 2387 | void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp) |
| 2388 | { |
| 2389 | MonFdset *mon_fdset; |
| 2390 | MonFdsetFd *mon_fdset_fd; |
| 2391 | char fd_str[60]; |
| 2392 | |
Peter Xu | 4745146 | 2018-06-08 11:55:11 +0800 | [diff] [blame] | 2393 | qemu_mutex_lock(&mon_fdsets_lock); |
Corey Bryant | ba1c048 | 2012-08-14 16:43:43 -0400 | [diff] [blame] | 2394 | QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { |
| 2395 | if (mon_fdset->id != fdset_id) { |
| 2396 | continue; |
| 2397 | } |
| 2398 | QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) { |
| 2399 | if (has_fd) { |
| 2400 | if (mon_fdset_fd->fd != fd) { |
| 2401 | continue; |
| 2402 | } |
| 2403 | mon_fdset_fd->removed = true; |
| 2404 | break; |
| 2405 | } else { |
| 2406 | mon_fdset_fd->removed = true; |
| 2407 | } |
| 2408 | } |
| 2409 | if (has_fd && !mon_fdset_fd) { |
| 2410 | goto error; |
| 2411 | } |
| 2412 | monitor_fdset_cleanup(mon_fdset); |
Peter Xu | 4745146 | 2018-06-08 11:55:11 +0800 | [diff] [blame] | 2413 | qemu_mutex_unlock(&mon_fdsets_lock); |
Corey Bryant | ba1c048 | 2012-08-14 16:43:43 -0400 | [diff] [blame] | 2414 | return; |
| 2415 | } |
| 2416 | |
| 2417 | error: |
Peter Xu | 4745146 | 2018-06-08 11:55:11 +0800 | [diff] [blame] | 2418 | qemu_mutex_unlock(&mon_fdsets_lock); |
Corey Bryant | ba1c048 | 2012-08-14 16:43:43 -0400 | [diff] [blame] | 2419 | if (has_fd) { |
| 2420 | snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64, |
| 2421 | fdset_id, fd); |
| 2422 | } else { |
| 2423 | snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id); |
| 2424 | } |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 2425 | error_setg(errp, QERR_FD_NOT_FOUND, fd_str); |
Corey Bryant | ba1c048 | 2012-08-14 16:43:43 -0400 | [diff] [blame] | 2426 | } |
| 2427 | |
| 2428 | FdsetInfoList *qmp_query_fdsets(Error **errp) |
| 2429 | { |
| 2430 | MonFdset *mon_fdset; |
| 2431 | MonFdsetFd *mon_fdset_fd; |
| 2432 | FdsetInfoList *fdset_list = NULL; |
| 2433 | |
Peter Xu | 4745146 | 2018-06-08 11:55:11 +0800 | [diff] [blame] | 2434 | qemu_mutex_lock(&mon_fdsets_lock); |
Corey Bryant | ba1c048 | 2012-08-14 16:43:43 -0400 | [diff] [blame] | 2435 | QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { |
| 2436 | FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info)); |
| 2437 | FdsetFdInfoList *fdsetfd_list = NULL; |
| 2438 | |
| 2439 | fdset_info->value = g_malloc0(sizeof(*fdset_info->value)); |
| 2440 | fdset_info->value->fdset_id = mon_fdset->id; |
| 2441 | |
| 2442 | QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) { |
| 2443 | FdsetFdInfoList *fdsetfd_info; |
| 2444 | |
| 2445 | fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info)); |
| 2446 | fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value)); |
| 2447 | fdsetfd_info->value->fd = mon_fdset_fd->fd; |
| 2448 | if (mon_fdset_fd->opaque) { |
| 2449 | fdsetfd_info->value->has_opaque = true; |
| 2450 | fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque); |
| 2451 | } else { |
| 2452 | fdsetfd_info->value->has_opaque = false; |
| 2453 | } |
| 2454 | |
| 2455 | fdsetfd_info->next = fdsetfd_list; |
| 2456 | fdsetfd_list = fdsetfd_info; |
| 2457 | } |
| 2458 | |
| 2459 | fdset_info->value->fds = fdsetfd_list; |
| 2460 | |
| 2461 | fdset_info->next = fdset_list; |
| 2462 | fdset_list = fdset_info; |
| 2463 | } |
Peter Xu | 4745146 | 2018-06-08 11:55:11 +0800 | [diff] [blame] | 2464 | qemu_mutex_unlock(&mon_fdsets_lock); |
Corey Bryant | ba1c048 | 2012-08-14 16:43:43 -0400 | [diff] [blame] | 2465 | |
| 2466 | return fdset_list; |
| 2467 | } |
| 2468 | |
Corey Bryant | e446f70 | 2012-10-18 15:19:32 -0400 | [diff] [blame] | 2469 | AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id, |
| 2470 | bool has_opaque, const char *opaque, |
| 2471 | Error **errp) |
| 2472 | { |
| 2473 | MonFdset *mon_fdset = NULL; |
| 2474 | MonFdsetFd *mon_fdset_fd; |
| 2475 | AddfdInfo *fdinfo; |
| 2476 | |
Peter Xu | 4745146 | 2018-06-08 11:55:11 +0800 | [diff] [blame] | 2477 | qemu_mutex_lock(&mon_fdsets_lock); |
Corey Bryant | e446f70 | 2012-10-18 15:19:32 -0400 | [diff] [blame] | 2478 | if (has_fdset_id) { |
| 2479 | QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { |
| 2480 | /* Break if match found or match impossible due to ordering by ID */ |
| 2481 | if (fdset_id <= mon_fdset->id) { |
| 2482 | if (fdset_id < mon_fdset->id) { |
| 2483 | mon_fdset = NULL; |
| 2484 | } |
| 2485 | break; |
| 2486 | } |
| 2487 | } |
| 2488 | } |
| 2489 | |
| 2490 | if (mon_fdset == NULL) { |
| 2491 | int64_t fdset_id_prev = -1; |
| 2492 | MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets); |
| 2493 | |
| 2494 | if (has_fdset_id) { |
| 2495 | if (fdset_id < 0) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 2496 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id", |
| 2497 | "a non-negative value"); |
Peter Xu | 4745146 | 2018-06-08 11:55:11 +0800 | [diff] [blame] | 2498 | qemu_mutex_unlock(&mon_fdsets_lock); |
Corey Bryant | e446f70 | 2012-10-18 15:19:32 -0400 | [diff] [blame] | 2499 | return NULL; |
| 2500 | } |
| 2501 | /* Use specified fdset ID */ |
| 2502 | QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { |
| 2503 | mon_fdset_cur = mon_fdset; |
| 2504 | if (fdset_id < mon_fdset_cur->id) { |
| 2505 | break; |
| 2506 | } |
| 2507 | } |
| 2508 | } else { |
| 2509 | /* Use first available fdset ID */ |
| 2510 | QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { |
| 2511 | mon_fdset_cur = mon_fdset; |
| 2512 | if (fdset_id_prev == mon_fdset_cur->id - 1) { |
| 2513 | fdset_id_prev = mon_fdset_cur->id; |
| 2514 | continue; |
| 2515 | } |
| 2516 | break; |
| 2517 | } |
| 2518 | } |
| 2519 | |
| 2520 | mon_fdset = g_malloc0(sizeof(*mon_fdset)); |
| 2521 | if (has_fdset_id) { |
| 2522 | mon_fdset->id = fdset_id; |
| 2523 | } else { |
| 2524 | mon_fdset->id = fdset_id_prev + 1; |
| 2525 | } |
| 2526 | |
| 2527 | /* The fdset list is ordered by fdset ID */ |
| 2528 | if (!mon_fdset_cur) { |
| 2529 | QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next); |
| 2530 | } else if (mon_fdset->id < mon_fdset_cur->id) { |
| 2531 | QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next); |
| 2532 | } else { |
| 2533 | QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next); |
| 2534 | } |
| 2535 | } |
| 2536 | |
| 2537 | mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd)); |
| 2538 | mon_fdset_fd->fd = fd; |
| 2539 | mon_fdset_fd->removed = false; |
| 2540 | if (has_opaque) { |
| 2541 | mon_fdset_fd->opaque = g_strdup(opaque); |
| 2542 | } |
| 2543 | QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next); |
| 2544 | |
| 2545 | fdinfo = g_malloc0(sizeof(*fdinfo)); |
| 2546 | fdinfo->fdset_id = mon_fdset->id; |
| 2547 | fdinfo->fd = mon_fdset_fd->fd; |
| 2548 | |
Peter Xu | 4745146 | 2018-06-08 11:55:11 +0800 | [diff] [blame] | 2549 | qemu_mutex_unlock(&mon_fdsets_lock); |
Corey Bryant | e446f70 | 2012-10-18 15:19:32 -0400 | [diff] [blame] | 2550 | return fdinfo; |
| 2551 | } |
| 2552 | |
Corey Bryant | adb696f | 2012-08-14 16:43:47 -0400 | [diff] [blame] | 2553 | int monitor_fdset_get_fd(int64_t fdset_id, int flags) |
| 2554 | { |
Peter Xu | 4745146 | 2018-06-08 11:55:11 +0800 | [diff] [blame] | 2555 | #ifdef _WIN32 |
| 2556 | return -ENOENT; |
| 2557 | #else |
Corey Bryant | adb696f | 2012-08-14 16:43:47 -0400 | [diff] [blame] | 2558 | MonFdset *mon_fdset; |
| 2559 | MonFdsetFd *mon_fdset_fd; |
| 2560 | int mon_fd_flags; |
Peter Xu | 4745146 | 2018-06-08 11:55:11 +0800 | [diff] [blame] | 2561 | int ret; |
Corey Bryant | adb696f | 2012-08-14 16:43:47 -0400 | [diff] [blame] | 2562 | |
Peter Xu | 4745146 | 2018-06-08 11:55:11 +0800 | [diff] [blame] | 2563 | qemu_mutex_lock(&mon_fdsets_lock); |
Corey Bryant | adb696f | 2012-08-14 16:43:47 -0400 | [diff] [blame] | 2564 | QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { |
| 2565 | if (mon_fdset->id != fdset_id) { |
| 2566 | continue; |
| 2567 | } |
| 2568 | QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) { |
| 2569 | mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL); |
| 2570 | if (mon_fd_flags == -1) { |
Peter Xu | 4745146 | 2018-06-08 11:55:11 +0800 | [diff] [blame] | 2571 | ret = -errno; |
| 2572 | goto out; |
Corey Bryant | adb696f | 2012-08-14 16:43:47 -0400 | [diff] [blame] | 2573 | } |
| 2574 | |
| 2575 | if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) { |
Peter Xu | 4745146 | 2018-06-08 11:55:11 +0800 | [diff] [blame] | 2576 | ret = mon_fdset_fd->fd; |
| 2577 | goto out; |
Corey Bryant | adb696f | 2012-08-14 16:43:47 -0400 | [diff] [blame] | 2578 | } |
| 2579 | } |
Peter Xu | 4745146 | 2018-06-08 11:55:11 +0800 | [diff] [blame] | 2580 | ret = -EACCES; |
| 2581 | goto out; |
Corey Bryant | adb696f | 2012-08-14 16:43:47 -0400 | [diff] [blame] | 2582 | } |
Peter Xu | 4745146 | 2018-06-08 11:55:11 +0800 | [diff] [blame] | 2583 | ret = -ENOENT; |
Corey Bryant | adb696f | 2012-08-14 16:43:47 -0400 | [diff] [blame] | 2584 | |
Peter Xu | 4745146 | 2018-06-08 11:55:11 +0800 | [diff] [blame] | 2585 | out: |
| 2586 | qemu_mutex_unlock(&mon_fdsets_lock); |
| 2587 | return ret; |
| 2588 | #endif |
Corey Bryant | adb696f | 2012-08-14 16:43:47 -0400 | [diff] [blame] | 2589 | } |
| 2590 | |
| 2591 | int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd) |
| 2592 | { |
| 2593 | MonFdset *mon_fdset; |
| 2594 | MonFdsetFd *mon_fdset_fd_dup; |
| 2595 | |
Peter Xu | 4745146 | 2018-06-08 11:55:11 +0800 | [diff] [blame] | 2596 | qemu_mutex_lock(&mon_fdsets_lock); |
Corey Bryant | adb696f | 2012-08-14 16:43:47 -0400 | [diff] [blame] | 2597 | QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { |
| 2598 | if (mon_fdset->id != fdset_id) { |
| 2599 | continue; |
| 2600 | } |
| 2601 | QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) { |
| 2602 | if (mon_fdset_fd_dup->fd == dup_fd) { |
Peter Xu | 4745146 | 2018-06-08 11:55:11 +0800 | [diff] [blame] | 2603 | goto err; |
Corey Bryant | adb696f | 2012-08-14 16:43:47 -0400 | [diff] [blame] | 2604 | } |
| 2605 | } |
| 2606 | mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup)); |
| 2607 | mon_fdset_fd_dup->fd = dup_fd; |
| 2608 | QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next); |
Peter Xu | 4745146 | 2018-06-08 11:55:11 +0800 | [diff] [blame] | 2609 | qemu_mutex_unlock(&mon_fdsets_lock); |
Corey Bryant | adb696f | 2012-08-14 16:43:47 -0400 | [diff] [blame] | 2610 | return 0; |
| 2611 | } |
Peter Xu | 4745146 | 2018-06-08 11:55:11 +0800 | [diff] [blame] | 2612 | |
| 2613 | err: |
| 2614 | qemu_mutex_unlock(&mon_fdsets_lock); |
Corey Bryant | adb696f | 2012-08-14 16:43:47 -0400 | [diff] [blame] | 2615 | return -1; |
| 2616 | } |
| 2617 | |
| 2618 | static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove) |
| 2619 | { |
| 2620 | MonFdset *mon_fdset; |
| 2621 | MonFdsetFd *mon_fdset_fd_dup; |
| 2622 | |
Peter Xu | 4745146 | 2018-06-08 11:55:11 +0800 | [diff] [blame] | 2623 | qemu_mutex_lock(&mon_fdsets_lock); |
Corey Bryant | adb696f | 2012-08-14 16:43:47 -0400 | [diff] [blame] | 2624 | QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { |
| 2625 | QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) { |
| 2626 | if (mon_fdset_fd_dup->fd == dup_fd) { |
| 2627 | if (remove) { |
| 2628 | QLIST_REMOVE(mon_fdset_fd_dup, next); |
| 2629 | if (QLIST_EMPTY(&mon_fdset->dup_fds)) { |
| 2630 | monitor_fdset_cleanup(mon_fdset); |
| 2631 | } |
Peter Xu | 4745146 | 2018-06-08 11:55:11 +0800 | [diff] [blame] | 2632 | goto err; |
Michael S. Tsirkin | b3dd1b8 | 2014-08-17 11:45:17 +0200 | [diff] [blame] | 2633 | } else { |
Peter Xu | 4745146 | 2018-06-08 11:55:11 +0800 | [diff] [blame] | 2634 | qemu_mutex_unlock(&mon_fdsets_lock); |
Michael S. Tsirkin | b3dd1b8 | 2014-08-17 11:45:17 +0200 | [diff] [blame] | 2635 | return mon_fdset->id; |
Corey Bryant | adb696f | 2012-08-14 16:43:47 -0400 | [diff] [blame] | 2636 | } |
Corey Bryant | adb696f | 2012-08-14 16:43:47 -0400 | [diff] [blame] | 2637 | } |
| 2638 | } |
| 2639 | } |
Peter Xu | 4745146 | 2018-06-08 11:55:11 +0800 | [diff] [blame] | 2640 | |
| 2641 | err: |
| 2642 | qemu_mutex_unlock(&mon_fdsets_lock); |
Corey Bryant | adb696f | 2012-08-14 16:43:47 -0400 | [diff] [blame] | 2643 | return -1; |
| 2644 | } |
| 2645 | |
| 2646 | int monitor_fdset_dup_fd_find(int dup_fd) |
| 2647 | { |
| 2648 | return monitor_fdset_dup_fd_find_remove(dup_fd, false); |
| 2649 | } |
| 2650 | |
Michael S. Tsirkin | b3dd1b8 | 2014-08-17 11:45:17 +0200 | [diff] [blame] | 2651 | void monitor_fdset_dup_fd_remove(int dup_fd) |
Corey Bryant | adb696f | 2012-08-14 16:43:47 -0400 | [diff] [blame] | 2652 | { |
Michael S. Tsirkin | b3dd1b8 | 2014-08-17 11:45:17 +0200 | [diff] [blame] | 2653 | monitor_fdset_dup_fd_find_remove(dup_fd, true); |
Corey Bryant | adb696f | 2012-08-14 16:43:47 -0400 | [diff] [blame] | 2654 | } |
| 2655 | |
Markus Armbruster | 1677f4c | 2015-02-09 14:03:19 +0100 | [diff] [blame] | 2656 | int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp) |
Laszlo Ersek | 5906366 | 2014-04-10 10:24:31 +0200 | [diff] [blame] | 2657 | { |
| 2658 | int fd; |
| 2659 | Error *local_err = NULL; |
| 2660 | |
| 2661 | if (!qemu_isdigit(fdname[0]) && mon) { |
Paolo Bonzini | a9940fc | 2012-09-20 16:50:32 +0200 | [diff] [blame] | 2662 | fd = monitor_get_fd(mon, fdname, &local_err); |
Nicholas Bellinger | a96ed02 | 2012-08-21 20:52:07 +0000 | [diff] [blame] | 2663 | } else { |
| 2664 | fd = qemu_parse_fd(fdname); |
Laszlo Ersek | 5906366 | 2014-04-10 10:24:31 +0200 | [diff] [blame] | 2665 | if (fd == -1) { |
| 2666 | error_setg(&local_err, "Invalid file descriptor number '%s'", |
| 2667 | fdname); |
| 2668 | } |
| 2669 | } |
| 2670 | if (local_err) { |
| 2671 | error_propagate(errp, local_err); |
| 2672 | assert(fd == -1); |
| 2673 | } else { |
| 2674 | assert(fd != -1); |
Nicholas Bellinger | a96ed02 | 2012-08-21 20:52:07 +0000 | [diff] [blame] | 2675 | } |
| 2676 | |
| 2677 | return fd; |
| 2678 | } |
| 2679 | |
Luiz Capitulino | acd0a09 | 2010-09-30 16:00:22 -0300 | [diff] [blame] | 2680 | /* Please update hmp-commands.hx when adding or changing commands */ |
Wayne Xia | 816f892 | 2011-10-12 11:32:41 +0800 | [diff] [blame] | 2681 | static mon_cmd_t info_cmds[] = { |
Pavel Butsykin | da76ee7 | 2015-09-10 18:38:58 +0300 | [diff] [blame] | 2682 | #include "hmp-commands-info.h" |
| 2683 | { NULL, NULL, }, |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 2684 | }; |
| 2685 | |
Wenchao Xia | a13ced5 | 2013-01-14 14:06:28 +0800 | [diff] [blame] | 2686 | /* mon_cmds and info_cmds would be sorted at runtime */ |
| 2687 | static mon_cmd_t mon_cmds[] = { |
| 2688 | #include "hmp-commands.h" |
| 2689 | { NULL, NULL, }, |
| 2690 | }; |
| 2691 | |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2692 | /*******************************************************************/ |
| 2693 | |
| 2694 | static const char *pch; |
Peter Maydell | 6ab7e54 | 2013-02-20 15:21:09 +0000 | [diff] [blame] | 2695 | static sigjmp_buf expr_env; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2696 | |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2697 | |
Stefan Weil | 9c3175c | 2013-08-22 21:30:09 +0200 | [diff] [blame] | 2698 | static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN |
| 2699 | expr_error(Monitor *mon, const char *fmt, ...) |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 2700 | { |
Fam Zheng | 277acfe | 2013-08-20 10:58:21 +0800 | [diff] [blame] | 2701 | va_list ap; |
| 2702 | va_start(ap, fmt); |
| 2703 | monitor_vprintf(mon, fmt, ap); |
| 2704 | monitor_printf(mon, "\n"); |
| 2705 | va_end(ap); |
Peter Maydell | 6ab7e54 | 2013-02-20 15:21:09 +0000 | [diff] [blame] | 2706 | siglongjmp(expr_env, 1); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2707 | } |
| 2708 | |
Markus Armbruster | 09b9418 | 2010-01-20 13:07:30 +0100 | [diff] [blame] | 2709 | /* return 0 if OK, -1 if not found */ |
bellard | 92a31b1 | 2005-02-10 22:00:52 +0000 | [diff] [blame] | 2710 | static int get_monitor_def(target_long *pval, const char *name) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2711 | { |
Pavel Butsykin | bf95728 | 2015-09-10 18:38:59 +0300 | [diff] [blame] | 2712 | const MonitorDef *md = target_monitor_defs(); |
Thomas Huth | 854e67f | 2017-01-13 13:12:35 +0100 | [diff] [blame] | 2713 | CPUState *cs = mon_get_cpu(); |
bellard | 92a31b1 | 2005-02-10 22:00:52 +0000 | [diff] [blame] | 2714 | void *ptr; |
Alexey Kardashevskiy | 0a9516c | 2015-11-12 14:44:23 +1100 | [diff] [blame] | 2715 | uint64_t tmp = 0; |
| 2716 | int ret; |
bellard | 92a31b1 | 2005-02-10 22:00:52 +0000 | [diff] [blame] | 2717 | |
Thomas Huth | 854e67f | 2017-01-13 13:12:35 +0100 | [diff] [blame] | 2718 | if (cs == NULL || md == NULL) { |
Pavel Butsykin | bf95728 | 2015-09-10 18:38:59 +0300 | [diff] [blame] | 2719 | return -1; |
| 2720 | } |
| 2721 | |
| 2722 | for(; md->name != NULL; md++) { |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2723 | if (compare_cmd(name, md->name)) { |
| 2724 | if (md->get_value) { |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 2725 | *pval = md->get_value(md, md->offset); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2726 | } else { |
Peter Crosthwaite | 5bcda5f | 2015-05-24 14:20:40 -0700 | [diff] [blame] | 2727 | CPUArchState *env = mon_get_cpu_env(); |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 2728 | ptr = (uint8_t *)env + md->offset; |
bellard | 92a31b1 | 2005-02-10 22:00:52 +0000 | [diff] [blame] | 2729 | switch(md->type) { |
| 2730 | case MD_I32: |
| 2731 | *pval = *(int32_t *)ptr; |
| 2732 | break; |
| 2733 | case MD_TLONG: |
| 2734 | *pval = *(target_long *)ptr; |
| 2735 | break; |
| 2736 | default: |
| 2737 | *pval = 0; |
| 2738 | break; |
| 2739 | } |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2740 | } |
| 2741 | return 0; |
| 2742 | } |
| 2743 | } |
Alexey Kardashevskiy | 0a9516c | 2015-11-12 14:44:23 +1100 | [diff] [blame] | 2744 | |
Thomas Huth | 854e67f | 2017-01-13 13:12:35 +0100 | [diff] [blame] | 2745 | ret = target_get_monitor_def(cs, name, &tmp); |
Alexey Kardashevskiy | 0a9516c | 2015-11-12 14:44:23 +1100 | [diff] [blame] | 2746 | if (!ret) { |
| 2747 | *pval = (target_long) tmp; |
| 2748 | } |
| 2749 | |
| 2750 | return ret; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2751 | } |
| 2752 | |
| 2753 | static void next(void) |
| 2754 | { |
Blue Swirl | 660f11b | 2009-07-31 21:16:51 +0000 | [diff] [blame] | 2755 | if (*pch != '\0') { |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2756 | pch++; |
blueswir1 | cd39008 | 2008-11-16 13:53:32 +0000 | [diff] [blame] | 2757 | while (qemu_isspace(*pch)) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2758 | pch++; |
| 2759 | } |
| 2760 | } |
| 2761 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2762 | static int64_t expr_sum(Monitor *mon); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2763 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2764 | static int64_t expr_unary(Monitor *mon) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2765 | { |
blueswir1 | c2efc95 | 2007-09-25 17:28:42 +0000 | [diff] [blame] | 2766 | int64_t n; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2767 | char *p; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 2768 | int ret; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2769 | |
| 2770 | switch(*pch) { |
| 2771 | case '+': |
| 2772 | next(); |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2773 | n = expr_unary(mon); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2774 | break; |
| 2775 | case '-': |
| 2776 | next(); |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2777 | n = -expr_unary(mon); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2778 | break; |
| 2779 | case '~': |
| 2780 | next(); |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2781 | n = ~expr_unary(mon); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2782 | break; |
| 2783 | case '(': |
| 2784 | next(); |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2785 | n = expr_sum(mon); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2786 | if (*pch != ')') { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2787 | expr_error(mon, "')' expected"); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2788 | } |
| 2789 | next(); |
| 2790 | break; |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 2791 | case '\'': |
| 2792 | pch++; |
| 2793 | if (*pch == '\0') |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2794 | expr_error(mon, "character constant expected"); |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 2795 | n = *pch; |
| 2796 | pch++; |
| 2797 | if (*pch != '\'') |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2798 | expr_error(mon, "missing terminating \' character"); |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 2799 | next(); |
| 2800 | break; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2801 | case '$': |
| 2802 | { |
| 2803 | char buf[128], *q; |
ths | 69b3497 | 2007-12-17 03:15:52 +0000 | [diff] [blame] | 2804 | target_long reg=0; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2805 | |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2806 | pch++; |
| 2807 | q = buf; |
| 2808 | while ((*pch >= 'a' && *pch <= 'z') || |
| 2809 | (*pch >= 'A' && *pch <= 'Z') || |
| 2810 | (*pch >= '0' && *pch <= '9') || |
bellard | 57206fd | 2004-04-25 18:54:52 +0000 | [diff] [blame] | 2811 | *pch == '_' || *pch == '.') { |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2812 | if ((q - buf) < sizeof(buf) - 1) |
| 2813 | *q++ = *pch; |
| 2814 | pch++; |
| 2815 | } |
blueswir1 | cd39008 | 2008-11-16 13:53:32 +0000 | [diff] [blame] | 2816 | while (qemu_isspace(*pch)) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2817 | pch++; |
| 2818 | *q = 0; |
blueswir1 | 7743e58 | 2007-09-24 18:39:04 +0000 | [diff] [blame] | 2819 | ret = get_monitor_def(®, buf); |
Markus Armbruster | 09b9418 | 2010-01-20 13:07:30 +0100 | [diff] [blame] | 2820 | if (ret < 0) |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2821 | expr_error(mon, "unknown register"); |
blueswir1 | 7743e58 | 2007-09-24 18:39:04 +0000 | [diff] [blame] | 2822 | n = reg; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2823 | } |
| 2824 | break; |
| 2825 | case '\0': |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2826 | expr_error(mon, "unexpected end of expression"); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2827 | n = 0; |
| 2828 | break; |
| 2829 | default: |
Luiz Capitulino | 6b0e33b | 2012-04-26 16:48:41 -0300 | [diff] [blame] | 2830 | errno = 0; |
bellard | 4f4fbf7 | 2006-06-25 18:28:12 +0000 | [diff] [blame] | 2831 | n = strtoull(pch, &p, 0); |
Luiz Capitulino | 6b0e33b | 2012-04-26 16:48:41 -0300 | [diff] [blame] | 2832 | if (errno == ERANGE) { |
| 2833 | expr_error(mon, "number too large"); |
| 2834 | } |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2835 | if (pch == p) { |
Fam Zheng | 277acfe | 2013-08-20 10:58:21 +0800 | [diff] [blame] | 2836 | expr_error(mon, "invalid char '%c' in expression", *p); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2837 | } |
| 2838 | pch = p; |
blueswir1 | cd39008 | 2008-11-16 13:53:32 +0000 | [diff] [blame] | 2839 | while (qemu_isspace(*pch)) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2840 | pch++; |
| 2841 | break; |
| 2842 | } |
| 2843 | return n; |
| 2844 | } |
| 2845 | |
| 2846 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2847 | static int64_t expr_prod(Monitor *mon) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2848 | { |
blueswir1 | c2efc95 | 2007-09-25 17:28:42 +0000 | [diff] [blame] | 2849 | int64_t val, val2; |
bellard | 92a31b1 | 2005-02-10 22:00:52 +0000 | [diff] [blame] | 2850 | int op; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2851 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2852 | val = expr_unary(mon); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2853 | for(;;) { |
| 2854 | op = *pch; |
| 2855 | if (op != '*' && op != '/' && op != '%') |
| 2856 | break; |
| 2857 | next(); |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2858 | val2 = expr_unary(mon); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2859 | switch(op) { |
| 2860 | default: |
| 2861 | case '*': |
| 2862 | val *= val2; |
| 2863 | break; |
| 2864 | case '/': |
| 2865 | case '%': |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2866 | if (val2 == 0) |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2867 | expr_error(mon, "division by zero"); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2868 | if (op == '/') |
| 2869 | val /= val2; |
| 2870 | else |
| 2871 | val %= val2; |
| 2872 | break; |
| 2873 | } |
| 2874 | } |
| 2875 | return val; |
| 2876 | } |
| 2877 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2878 | static int64_t expr_logic(Monitor *mon) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2879 | { |
blueswir1 | c2efc95 | 2007-09-25 17:28:42 +0000 | [diff] [blame] | 2880 | int64_t val, val2; |
bellard | 92a31b1 | 2005-02-10 22:00:52 +0000 | [diff] [blame] | 2881 | int op; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2882 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2883 | val = expr_prod(mon); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2884 | for(;;) { |
| 2885 | op = *pch; |
| 2886 | if (op != '&' && op != '|' && op != '^') |
| 2887 | break; |
| 2888 | next(); |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2889 | val2 = expr_prod(mon); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2890 | switch(op) { |
| 2891 | default: |
| 2892 | case '&': |
| 2893 | val &= val2; |
| 2894 | break; |
| 2895 | case '|': |
| 2896 | val |= val2; |
| 2897 | break; |
| 2898 | case '^': |
| 2899 | val ^= val2; |
| 2900 | break; |
| 2901 | } |
| 2902 | } |
| 2903 | return val; |
| 2904 | } |
| 2905 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2906 | static int64_t expr_sum(Monitor *mon) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2907 | { |
blueswir1 | c2efc95 | 2007-09-25 17:28:42 +0000 | [diff] [blame] | 2908 | int64_t val, val2; |
bellard | 92a31b1 | 2005-02-10 22:00:52 +0000 | [diff] [blame] | 2909 | int op; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2910 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2911 | val = expr_logic(mon); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2912 | for(;;) { |
| 2913 | op = *pch; |
| 2914 | if (op != '+' && op != '-') |
| 2915 | break; |
| 2916 | next(); |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2917 | val2 = expr_logic(mon); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2918 | if (op == '+') |
| 2919 | val += val2; |
| 2920 | else |
| 2921 | val -= val2; |
| 2922 | } |
| 2923 | return val; |
| 2924 | } |
| 2925 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2926 | static int get_expr(Monitor *mon, int64_t *pval, const char **pp) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2927 | { |
| 2928 | pch = *pp; |
Peter Maydell | 6ab7e54 | 2013-02-20 15:21:09 +0000 | [diff] [blame] | 2929 | if (sigsetjmp(expr_env, 0)) { |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2930 | *pp = pch; |
| 2931 | return -1; |
| 2932 | } |
blueswir1 | cd39008 | 2008-11-16 13:53:32 +0000 | [diff] [blame] | 2933 | while (qemu_isspace(*pch)) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2934 | pch++; |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2935 | *pval = expr_sum(mon); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2936 | *pp = pch; |
| 2937 | return 0; |
| 2938 | } |
| 2939 | |
Markus Armbruster | 3350a4d | 2010-01-25 14:23:03 +0100 | [diff] [blame] | 2940 | static int get_double(Monitor *mon, double *pval, const char **pp) |
| 2941 | { |
| 2942 | const char *p = *pp; |
| 2943 | char *tailp; |
| 2944 | double d; |
| 2945 | |
| 2946 | d = strtod(p, &tailp); |
| 2947 | if (tailp == p) { |
| 2948 | monitor_printf(mon, "Number expected\n"); |
| 2949 | return -1; |
| 2950 | } |
| 2951 | if (d != d || d - d != 0) { |
| 2952 | /* NaN or infinity */ |
| 2953 | monitor_printf(mon, "Bad number\n"); |
| 2954 | return -1; |
| 2955 | } |
| 2956 | *pval = d; |
| 2957 | *pp = tailp; |
| 2958 | return 0; |
| 2959 | } |
| 2960 | |
Luiz Capitulino | 4590fd8 | 2009-06-09 18:21:30 -0300 | [diff] [blame] | 2961 | /* |
| 2962 | * Store the command-name in cmdname, and return a pointer to |
| 2963 | * the remaining of the command string. |
| 2964 | */ |
| 2965 | static const char *get_command_name(const char *cmdline, |
| 2966 | char *cmdname, size_t nlen) |
| 2967 | { |
| 2968 | size_t len; |
| 2969 | const char *p, *pstart; |
| 2970 | |
| 2971 | p = cmdline; |
| 2972 | while (qemu_isspace(*p)) |
| 2973 | p++; |
| 2974 | if (*p == '\0') |
| 2975 | return NULL; |
| 2976 | pstart = p; |
| 2977 | while (*p != '\0' && *p != '/' && !qemu_isspace(*p)) |
| 2978 | p++; |
| 2979 | len = p - pstart; |
| 2980 | if (len > nlen - 1) |
| 2981 | len = nlen - 1; |
| 2982 | memcpy(cmdname, pstart, len); |
| 2983 | cmdname[len] = '\0'; |
| 2984 | return p; |
| 2985 | } |
| 2986 | |
Luiz Capitulino | 4d76d2b | 2009-08-28 15:27:09 -0300 | [diff] [blame] | 2987 | /** |
| 2988 | * Read key of 'type' into 'key' and return the current |
| 2989 | * 'type' pointer. |
| 2990 | */ |
| 2991 | static char *key_get_info(const char *type, char **key) |
| 2992 | { |
| 2993 | size_t len; |
| 2994 | char *p, *str; |
| 2995 | |
| 2996 | if (*type == ',') |
| 2997 | type++; |
| 2998 | |
| 2999 | p = strchr(type, ':'); |
| 3000 | if (!p) { |
| 3001 | *key = NULL; |
| 3002 | return NULL; |
| 3003 | } |
| 3004 | len = p - type; |
| 3005 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 3006 | str = g_malloc(len + 1); |
Luiz Capitulino | 4d76d2b | 2009-08-28 15:27:09 -0300 | [diff] [blame] | 3007 | memcpy(str, type, len); |
| 3008 | str[len] = '\0'; |
| 3009 | |
| 3010 | *key = str; |
| 3011 | return ++p; |
| 3012 | } |
| 3013 | |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 3014 | static int default_fmt_format = 'x'; |
| 3015 | static int default_fmt_size = 4; |
| 3016 | |
lirans@il.ibm.com | fbc3d96 | 2009-11-02 15:41:13 +0200 | [diff] [blame] | 3017 | static int is_valid_option(const char *c, const char *typestr) |
| 3018 | { |
| 3019 | char option[3]; |
| 3020 | |
| 3021 | option[0] = '-'; |
| 3022 | option[1] = *c; |
| 3023 | option[2] = '\0'; |
| 3024 | |
| 3025 | typestr = strstr(typestr, option); |
| 3026 | return (typestr != NULL); |
| 3027 | } |
| 3028 | |
Luiz Capitulino | 945c5ac | 2010-09-13 13:17:58 -0300 | [diff] [blame] | 3029 | static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table, |
| 3030 | const char *cmdname) |
Luiz Capitulino | 7fd669a | 2009-11-26 22:58:54 -0200 | [diff] [blame] | 3031 | { |
| 3032 | const mon_cmd_t *cmd; |
| 3033 | |
Luiz Capitulino | 945c5ac | 2010-09-13 13:17:58 -0300 | [diff] [blame] | 3034 | for (cmd = disp_table; cmd->name != NULL; cmd++) { |
Luiz Capitulino | 7fd669a | 2009-11-26 22:58:54 -0200 | [diff] [blame] | 3035 | if (compare_cmd(cmdname, cmd->name)) { |
| 3036 | return cmd; |
| 3037 | } |
| 3038 | } |
| 3039 | |
| 3040 | return NULL; |
| 3041 | } |
| 3042 | |
Wenchao Xia | 5f3d335 | 2013-01-14 14:06:27 +0800 | [diff] [blame] | 3043 | /* |
Bandan Das | ae50212 | 2015-06-03 18:38:08 -0400 | [diff] [blame] | 3044 | * Parse command name from @cmdp according to command table @table. |
| 3045 | * If blank, return NULL. |
| 3046 | * Else, if no valid command can be found, report to @mon, and return |
| 3047 | * NULL. |
| 3048 | * Else, change @cmdp to point right behind the name, and return its |
| 3049 | * command table entry. |
| 3050 | * Do not assume the return value points into @table! It doesn't when |
| 3051 | * the command is found in a sub-command table. |
Wenchao Xia | 5f3d335 | 2013-01-14 14:06:27 +0800 | [diff] [blame] | 3052 | */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3053 | static const mon_cmd_t *monitor_parse_command(Monitor *mon, |
Dr. David Alan Gilbert | 250b819 | 2017-08-17 11:42:16 +0100 | [diff] [blame] | 3054 | const char *cmdp_start, |
Bandan Das | ae50212 | 2015-06-03 18:38:08 -0400 | [diff] [blame] | 3055 | const char **cmdp, |
| 3056 | mon_cmd_t *table) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 3057 | { |
Bandan Das | ae50212 | 2015-06-03 18:38:08 -0400 | [diff] [blame] | 3058 | const char *p; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3059 | const mon_cmd_t *cmd; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 3060 | char cmdname[256]; |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 3061 | |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 3062 | /* extract the command name */ |
Bandan Das | ae50212 | 2015-06-03 18:38:08 -0400 | [diff] [blame] | 3063 | p = get_command_name(*cmdp, cmdname, sizeof(cmdname)); |
Luiz Capitulino | 4590fd8 | 2009-06-09 18:21:30 -0300 | [diff] [blame] | 3064 | if (!p) |
Luiz Capitulino | 55f81d9 | 2009-08-28 15:27:22 -0300 | [diff] [blame] | 3065 | return NULL; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3066 | |
Wenchao Xia | 5f3d335 | 2013-01-14 14:06:27 +0800 | [diff] [blame] | 3067 | cmd = search_dispatch_table(table, cmdname); |
Luiz Capitulino | 7fd669a | 2009-11-26 22:58:54 -0200 | [diff] [blame] | 3068 | if (!cmd) { |
Wenchao Xia | 5f3d335 | 2013-01-14 14:06:27 +0800 | [diff] [blame] | 3069 | monitor_printf(mon, "unknown command: '%.*s'\n", |
Dr. David Alan Gilbert | 250b819 | 2017-08-17 11:42:16 +0100 | [diff] [blame] | 3070 | (int)(p - cmdp_start), cmdp_start); |
Luiz Capitulino | 55f81d9 | 2009-08-28 15:27:22 -0300 | [diff] [blame] | 3071 | return NULL; |
Luiz Capitulino | d91d9bf | 2009-06-09 18:21:54 -0300 | [diff] [blame] | 3072 | } |
Dr. David Alan Gilbert | c3120f7 | 2018-06-20 16:39:41 +0100 | [diff] [blame] | 3073 | if (runstate_check(RUN_STATE_PRECONFIG) && !cmd_can_preconfig(cmd)) { |
| 3074 | monitor_printf(mon, "Command '%.*s' not available with -preconfig " |
| 3075 | "until after exit_preconfig.\n", |
| 3076 | (int)(p - cmdp_start), cmdp_start); |
| 3077 | return NULL; |
| 3078 | } |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 3079 | |
Wenchao Xia | 5f3d335 | 2013-01-14 14:06:27 +0800 | [diff] [blame] | 3080 | /* filter out following useless space */ |
| 3081 | while (qemu_isspace(*p)) { |
| 3082 | p++; |
| 3083 | } |
Bandan Das | ae50212 | 2015-06-03 18:38:08 -0400 | [diff] [blame] | 3084 | |
| 3085 | *cmdp = p; |
Wenchao Xia | 5f3d335 | 2013-01-14 14:06:27 +0800 | [diff] [blame] | 3086 | /* search sub command */ |
Bandan Das | ae50212 | 2015-06-03 18:38:08 -0400 | [diff] [blame] | 3087 | if (cmd->sub_table != NULL && *p != '\0') { |
Dr. David Alan Gilbert | 250b819 | 2017-08-17 11:42:16 +0100 | [diff] [blame] | 3088 | return monitor_parse_command(mon, cmdp_start, cmdp, cmd->sub_table); |
Wenchao Xia | 5f3d335 | 2013-01-14 14:06:27 +0800 | [diff] [blame] | 3089 | } |
| 3090 | |
Bandan Das | ae50212 | 2015-06-03 18:38:08 -0400 | [diff] [blame] | 3091 | return cmd; |
| 3092 | } |
| 3093 | |
| 3094 | /* |
| 3095 | * Parse arguments for @cmd. |
| 3096 | * If it can't be parsed, report to @mon, and return NULL. |
| 3097 | * Else, insert command arguments into a QDict, and return it. |
| 3098 | * Note: On success, caller has to free the QDict structure. |
| 3099 | */ |
| 3100 | |
| 3101 | static QDict *monitor_parse_arguments(Monitor *mon, |
| 3102 | const char **endp, |
| 3103 | const mon_cmd_t *cmd) |
| 3104 | { |
| 3105 | const char *typestr; |
| 3106 | char *key; |
| 3107 | int c; |
| 3108 | const char *p = *endp; |
| 3109 | char buf[1024]; |
| 3110 | QDict *qdict = qdict_new(); |
| 3111 | |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 3112 | /* parse the parameters */ |
| 3113 | typestr = cmd->args_type; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 3114 | for(;;) { |
Luiz Capitulino | 4d76d2b | 2009-08-28 15:27:09 -0300 | [diff] [blame] | 3115 | typestr = key_get_info(typestr, &key); |
| 3116 | if (!typestr) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 3117 | break; |
Luiz Capitulino | 4d76d2b | 2009-08-28 15:27:09 -0300 | [diff] [blame] | 3118 | c = *typestr; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 3119 | typestr++; |
| 3120 | switch(c) { |
| 3121 | case 'F': |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 3122 | case 'B': |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 3123 | case 's': |
| 3124 | { |
| 3125 | int ret; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3126 | |
blueswir1 | cd39008 | 2008-11-16 13:53:32 +0000 | [diff] [blame] | 3127 | while (qemu_isspace(*p)) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 3128 | p++; |
| 3129 | if (*typestr == '?') { |
| 3130 | typestr++; |
| 3131 | if (*p == '\0') { |
| 3132 | /* no optional string: NULL argument */ |
Luiz Capitulino | 5377358 | 2009-08-28 15:27:25 -0300 | [diff] [blame] | 3133 | break; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 3134 | } |
| 3135 | } |
| 3136 | ret = get_str(buf, sizeof(buf), &p); |
| 3137 | if (ret < 0) { |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 3138 | switch(c) { |
| 3139 | case 'F': |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 3140 | monitor_printf(mon, "%s: filename expected\n", |
Bandan Das | ae50212 | 2015-06-03 18:38:08 -0400 | [diff] [blame] | 3141 | cmd->name); |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 3142 | break; |
| 3143 | case 'B': |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 3144 | monitor_printf(mon, "%s: block device name expected\n", |
Bandan Das | ae50212 | 2015-06-03 18:38:08 -0400 | [diff] [blame] | 3145 | cmd->name); |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 3146 | break; |
| 3147 | default: |
Bandan Das | ae50212 | 2015-06-03 18:38:08 -0400 | [diff] [blame] | 3148 | monitor_printf(mon, "%s: string expected\n", cmd->name); |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 3149 | break; |
| 3150 | } |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 3151 | goto fail; |
| 3152 | } |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 3153 | qdict_put_str(qdict, key, buf); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 3154 | } |
| 3155 | break; |
Markus Armbruster | 361127d | 2010-02-10 20:24:35 +0100 | [diff] [blame] | 3156 | case 'O': |
| 3157 | { |
| 3158 | QemuOptsList *opts_list; |
| 3159 | QemuOpts *opts; |
| 3160 | |
| 3161 | opts_list = qemu_find_opts(key); |
| 3162 | if (!opts_list || opts_list->desc->name) { |
| 3163 | goto bad_type; |
| 3164 | } |
| 3165 | while (qemu_isspace(*p)) { |
| 3166 | p++; |
| 3167 | } |
| 3168 | if (!*p) |
| 3169 | break; |
| 3170 | if (get_str(buf, sizeof(buf), &p) < 0) { |
| 3171 | goto fail; |
| 3172 | } |
Markus Armbruster | 70b9433 | 2015-02-13 12:50:26 +0100 | [diff] [blame] | 3173 | opts = qemu_opts_parse_noisily(opts_list, buf, true); |
Markus Armbruster | 361127d | 2010-02-10 20:24:35 +0100 | [diff] [blame] | 3174 | if (!opts) { |
| 3175 | goto fail; |
| 3176 | } |
| 3177 | qemu_opts_to_qdict(opts, qdict); |
| 3178 | qemu_opts_del(opts); |
| 3179 | } |
| 3180 | break; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 3181 | case '/': |
| 3182 | { |
| 3183 | int count, format, size; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3184 | |
blueswir1 | cd39008 | 2008-11-16 13:53:32 +0000 | [diff] [blame] | 3185 | while (qemu_isspace(*p)) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 3186 | p++; |
| 3187 | if (*p == '/') { |
| 3188 | /* format found */ |
| 3189 | p++; |
| 3190 | count = 1; |
blueswir1 | cd39008 | 2008-11-16 13:53:32 +0000 | [diff] [blame] | 3191 | if (qemu_isdigit(*p)) { |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 3192 | count = 0; |
blueswir1 | cd39008 | 2008-11-16 13:53:32 +0000 | [diff] [blame] | 3193 | while (qemu_isdigit(*p)) { |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 3194 | count = count * 10 + (*p - '0'); |
| 3195 | p++; |
| 3196 | } |
| 3197 | } |
| 3198 | size = -1; |
| 3199 | format = -1; |
| 3200 | for(;;) { |
| 3201 | switch(*p) { |
| 3202 | case 'o': |
| 3203 | case 'd': |
| 3204 | case 'u': |
| 3205 | case 'x': |
| 3206 | case 'i': |
| 3207 | case 'c': |
| 3208 | format = *p++; |
| 3209 | break; |
| 3210 | case 'b': |
| 3211 | size = 1; |
| 3212 | p++; |
| 3213 | break; |
| 3214 | case 'h': |
| 3215 | size = 2; |
| 3216 | p++; |
| 3217 | break; |
| 3218 | case 'w': |
| 3219 | size = 4; |
| 3220 | p++; |
| 3221 | break; |
| 3222 | case 'g': |
| 3223 | case 'L': |
| 3224 | size = 8; |
| 3225 | p++; |
| 3226 | break; |
| 3227 | default: |
| 3228 | goto next; |
| 3229 | } |
| 3230 | } |
| 3231 | next: |
blueswir1 | cd39008 | 2008-11-16 13:53:32 +0000 | [diff] [blame] | 3232 | if (*p != '\0' && !qemu_isspace(*p)) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 3233 | monitor_printf(mon, "invalid char in format: '%c'\n", |
| 3234 | *p); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 3235 | goto fail; |
| 3236 | } |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 3237 | if (format < 0) |
| 3238 | format = default_fmt_format; |
bellard | 4c27ba2 | 2004-04-25 18:05:08 +0000 | [diff] [blame] | 3239 | if (format != 'i') { |
| 3240 | /* for 'i', not specifying a size gives -1 as size */ |
| 3241 | if (size < 0) |
| 3242 | size = default_fmt_size; |
aurel32 | e90f009 | 2008-10-01 21:45:51 +0000 | [diff] [blame] | 3243 | default_fmt_size = size; |
bellard | 4c27ba2 | 2004-04-25 18:05:08 +0000 | [diff] [blame] | 3244 | } |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 3245 | default_fmt_format = format; |
| 3246 | } else { |
| 3247 | count = 1; |
| 3248 | format = default_fmt_format; |
bellard | 4c27ba2 | 2004-04-25 18:05:08 +0000 | [diff] [blame] | 3249 | if (format != 'i') { |
| 3250 | size = default_fmt_size; |
| 3251 | } else { |
| 3252 | size = -1; |
| 3253 | } |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 3254 | } |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 3255 | qdict_put_int(qdict, "count", count); |
| 3256 | qdict_put_int(qdict, "format", format); |
| 3257 | qdict_put_int(qdict, "size", size); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 3258 | } |
| 3259 | break; |
| 3260 | case 'i': |
bellard | 92a31b1 | 2005-02-10 22:00:52 +0000 | [diff] [blame] | 3261 | case 'l': |
Luiz Capitulino | b6e098d | 2009-12-18 13:25:04 -0200 | [diff] [blame] | 3262 | case 'M': |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 3263 | { |
blueswir1 | c2efc95 | 2007-09-25 17:28:42 +0000 | [diff] [blame] | 3264 | int64_t val; |
blueswir1 | 7743e58 | 2007-09-24 18:39:04 +0000 | [diff] [blame] | 3265 | |
blueswir1 | cd39008 | 2008-11-16 13:53:32 +0000 | [diff] [blame] | 3266 | while (qemu_isspace(*p)) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 3267 | p++; |
bellard | 3440557 | 2004-06-08 00:55:58 +0000 | [diff] [blame] | 3268 | if (*typestr == '?' || *typestr == '.') { |
bellard | 3440557 | 2004-06-08 00:55:58 +0000 | [diff] [blame] | 3269 | if (*typestr == '?') { |
Luiz Capitulino | 5377358 | 2009-08-28 15:27:25 -0300 | [diff] [blame] | 3270 | if (*p == '\0') { |
| 3271 | typestr++; |
| 3272 | break; |
| 3273 | } |
bellard | 3440557 | 2004-06-08 00:55:58 +0000 | [diff] [blame] | 3274 | } else { |
| 3275 | if (*p == '.') { |
| 3276 | p++; |
blueswir1 | cd39008 | 2008-11-16 13:53:32 +0000 | [diff] [blame] | 3277 | while (qemu_isspace(*p)) |
bellard | 3440557 | 2004-06-08 00:55:58 +0000 | [diff] [blame] | 3278 | p++; |
bellard | 3440557 | 2004-06-08 00:55:58 +0000 | [diff] [blame] | 3279 | } else { |
Luiz Capitulino | 5377358 | 2009-08-28 15:27:25 -0300 | [diff] [blame] | 3280 | typestr++; |
| 3281 | break; |
bellard | 3440557 | 2004-06-08 00:55:58 +0000 | [diff] [blame] | 3282 | } |
| 3283 | } |
bellard | 13224a8 | 2006-07-14 22:03:35 +0000 | [diff] [blame] | 3284 | typestr++; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 3285 | } |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 3286 | if (get_expr(mon, &val, &p)) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 3287 | goto fail; |
Luiz Capitulino | 675ebef | 2009-08-28 15:27:26 -0300 | [diff] [blame] | 3288 | /* Check if 'i' is greater than 32-bit */ |
| 3289 | if ((c == 'i') && ((val >> 32) & 0xffffffff)) { |
Bandan Das | ae50212 | 2015-06-03 18:38:08 -0400 | [diff] [blame] | 3290 | monitor_printf(mon, "\'%s\' has failed: ", cmd->name); |
Luiz Capitulino | 675ebef | 2009-08-28 15:27:26 -0300 | [diff] [blame] | 3291 | monitor_printf(mon, "integer is for 32-bit values\n"); |
| 3292 | goto fail; |
Luiz Capitulino | b6e098d | 2009-12-18 13:25:04 -0200 | [diff] [blame] | 3293 | } else if (c == 'M') { |
Luiz Capitulino | 9116284 | 2012-04-26 17:34:30 -0300 | [diff] [blame] | 3294 | if (val < 0) { |
| 3295 | monitor_printf(mon, "enter a positive value\n"); |
| 3296 | goto fail; |
| 3297 | } |
Philippe Mathieu-Daudé | 8ec338a | 2018-06-25 09:42:34 -0300 | [diff] [blame] | 3298 | val *= MiB; |
Luiz Capitulino | 675ebef | 2009-08-28 15:27:26 -0300 | [diff] [blame] | 3299 | } |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 3300 | qdict_put_int(qdict, key, val); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 3301 | } |
| 3302 | break; |
Jes Sorensen | dbc0c67 | 2010-10-21 17:15:47 +0200 | [diff] [blame] | 3303 | case 'o': |
| 3304 | { |
Markus Armbruster | f17fd4f | 2017-02-21 21:14:06 +0100 | [diff] [blame] | 3305 | int ret; |
Markus Armbruster | f46bfdb | 2017-02-21 21:14:07 +0100 | [diff] [blame] | 3306 | uint64_t val; |
Jes Sorensen | dbc0c67 | 2010-10-21 17:15:47 +0200 | [diff] [blame] | 3307 | char *end; |
| 3308 | |
| 3309 | while (qemu_isspace(*p)) { |
| 3310 | p++; |
| 3311 | } |
| 3312 | if (*typestr == '?') { |
| 3313 | typestr++; |
| 3314 | if (*p == '\0') { |
| 3315 | break; |
| 3316 | } |
| 3317 | } |
Markus Armbruster | f17fd4f | 2017-02-21 21:14:06 +0100 | [diff] [blame] | 3318 | ret = qemu_strtosz_MiB(p, &end, &val); |
Markus Armbruster | f46bfdb | 2017-02-21 21:14:07 +0100 | [diff] [blame] | 3319 | if (ret < 0 || val > INT64_MAX) { |
Jes Sorensen | dbc0c67 | 2010-10-21 17:15:47 +0200 | [diff] [blame] | 3320 | monitor_printf(mon, "invalid size\n"); |
| 3321 | goto fail; |
| 3322 | } |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 3323 | qdict_put_int(qdict, key, val); |
Jes Sorensen | dbc0c67 | 2010-10-21 17:15:47 +0200 | [diff] [blame] | 3324 | p = end; |
| 3325 | } |
| 3326 | break; |
Markus Armbruster | fccfb11e | 2010-01-25 14:23:06 +0100 | [diff] [blame] | 3327 | case 'T': |
Markus Armbruster | 3350a4d | 2010-01-25 14:23:03 +0100 | [diff] [blame] | 3328 | { |
| 3329 | double val; |
| 3330 | |
| 3331 | while (qemu_isspace(*p)) |
| 3332 | p++; |
| 3333 | if (*typestr == '?') { |
| 3334 | typestr++; |
| 3335 | if (*p == '\0') { |
| 3336 | break; |
| 3337 | } |
| 3338 | } |
| 3339 | if (get_double(mon, &val, &p) < 0) { |
| 3340 | goto fail; |
| 3341 | } |
Jes Sorensen | 07de3e6 | 2010-10-21 17:15:49 +0200 | [diff] [blame] | 3342 | if (p[0] && p[1] == 's') { |
Markus Armbruster | fccfb11e | 2010-01-25 14:23:06 +0100 | [diff] [blame] | 3343 | switch (*p) { |
| 3344 | case 'm': |
| 3345 | val /= 1e3; p += 2; break; |
| 3346 | case 'u': |
| 3347 | val /= 1e6; p += 2; break; |
| 3348 | case 'n': |
| 3349 | val /= 1e9; p += 2; break; |
| 3350 | } |
| 3351 | } |
Markus Armbruster | 3350a4d | 2010-01-25 14:23:03 +0100 | [diff] [blame] | 3352 | if (*p && !qemu_isspace(*p)) { |
| 3353 | monitor_printf(mon, "Unknown unit suffix\n"); |
| 3354 | goto fail; |
| 3355 | } |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 3356 | qdict_put(qdict, key, qnum_from_double(val)); |
Markus Armbruster | 3350a4d | 2010-01-25 14:23:03 +0100 | [diff] [blame] | 3357 | } |
| 3358 | break; |
Markus Armbruster | 942cd1f | 2010-03-26 09:07:09 +0100 | [diff] [blame] | 3359 | case 'b': |
| 3360 | { |
| 3361 | const char *beg; |
Eric Blake | fc48ffc | 2015-05-15 16:24:59 -0600 | [diff] [blame] | 3362 | bool val; |
Markus Armbruster | 942cd1f | 2010-03-26 09:07:09 +0100 | [diff] [blame] | 3363 | |
| 3364 | while (qemu_isspace(*p)) { |
| 3365 | p++; |
| 3366 | } |
| 3367 | beg = p; |
| 3368 | while (qemu_isgraph(*p)) { |
| 3369 | p++; |
| 3370 | } |
| 3371 | if (p - beg == 2 && !memcmp(beg, "on", p - beg)) { |
Eric Blake | fc48ffc | 2015-05-15 16:24:59 -0600 | [diff] [blame] | 3372 | val = true; |
Markus Armbruster | 942cd1f | 2010-03-26 09:07:09 +0100 | [diff] [blame] | 3373 | } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) { |
Eric Blake | fc48ffc | 2015-05-15 16:24:59 -0600 | [diff] [blame] | 3374 | val = false; |
Markus Armbruster | 942cd1f | 2010-03-26 09:07:09 +0100 | [diff] [blame] | 3375 | } else { |
| 3376 | monitor_printf(mon, "Expected 'on' or 'off'\n"); |
| 3377 | goto fail; |
| 3378 | } |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 3379 | qdict_put_bool(qdict, key, val); |
Markus Armbruster | 942cd1f | 2010-03-26 09:07:09 +0100 | [diff] [blame] | 3380 | } |
| 3381 | break; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 3382 | case '-': |
| 3383 | { |
lirans@il.ibm.com | fbc3d96 | 2009-11-02 15:41:13 +0200 | [diff] [blame] | 3384 | const char *tmp = p; |
Luiz Capitulino | eb159d1 | 2010-05-28 15:25:24 -0300 | [diff] [blame] | 3385 | int skip_key = 0; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 3386 | /* option */ |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3387 | |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 3388 | c = *typestr++; |
| 3389 | if (c == '\0') |
| 3390 | goto bad_type; |
blueswir1 | cd39008 | 2008-11-16 13:53:32 +0000 | [diff] [blame] | 3391 | while (qemu_isspace(*p)) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 3392 | p++; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 3393 | if (*p == '-') { |
| 3394 | p++; |
lirans@il.ibm.com | fbc3d96 | 2009-11-02 15:41:13 +0200 | [diff] [blame] | 3395 | if(c != *p) { |
| 3396 | if(!is_valid_option(p, typestr)) { |
| 3397 | |
| 3398 | monitor_printf(mon, "%s: unsupported option -%c\n", |
Bandan Das | ae50212 | 2015-06-03 18:38:08 -0400 | [diff] [blame] | 3399 | cmd->name, *p); |
lirans@il.ibm.com | fbc3d96 | 2009-11-02 15:41:13 +0200 | [diff] [blame] | 3400 | goto fail; |
| 3401 | } else { |
| 3402 | skip_key = 1; |
| 3403 | } |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 3404 | } |
lirans@il.ibm.com | fbc3d96 | 2009-11-02 15:41:13 +0200 | [diff] [blame] | 3405 | if(skip_key) { |
| 3406 | p = tmp; |
| 3407 | } else { |
Luiz Capitulino | eb159d1 | 2010-05-28 15:25:24 -0300 | [diff] [blame] | 3408 | /* has option */ |
lirans@il.ibm.com | fbc3d96 | 2009-11-02 15:41:13 +0200 | [diff] [blame] | 3409 | p++; |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 3410 | qdict_put_bool(qdict, key, true); |
lirans@il.ibm.com | fbc3d96 | 2009-11-02 15:41:13 +0200 | [diff] [blame] | 3411 | } |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 3412 | } |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 3413 | } |
| 3414 | break; |
Wenchao Xia | 129be00 | 2013-08-27 20:38:26 +0800 | [diff] [blame] | 3415 | case 'S': |
| 3416 | { |
| 3417 | /* package all remaining string */ |
| 3418 | int len; |
| 3419 | |
| 3420 | while (qemu_isspace(*p)) { |
| 3421 | p++; |
| 3422 | } |
| 3423 | if (*typestr == '?') { |
| 3424 | typestr++; |
| 3425 | if (*p == '\0') { |
| 3426 | /* no remaining string: NULL argument */ |
| 3427 | break; |
| 3428 | } |
| 3429 | } |
| 3430 | len = strlen(p); |
| 3431 | if (len <= 0) { |
| 3432 | monitor_printf(mon, "%s: string expected\n", |
Bandan Das | ae50212 | 2015-06-03 18:38:08 -0400 | [diff] [blame] | 3433 | cmd->name); |
Bandan Das | e549d2a | 2015-06-03 18:38:10 -0400 | [diff] [blame] | 3434 | goto fail; |
Wenchao Xia | 129be00 | 2013-08-27 20:38:26 +0800 | [diff] [blame] | 3435 | } |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 3436 | qdict_put_str(qdict, key, p); |
Wenchao Xia | 129be00 | 2013-08-27 20:38:26 +0800 | [diff] [blame] | 3437 | p += len; |
| 3438 | } |
| 3439 | break; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 3440 | default: |
| 3441 | bad_type: |
Bandan Das | ae50212 | 2015-06-03 18:38:08 -0400 | [diff] [blame] | 3442 | monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 3443 | goto fail; |
| 3444 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 3445 | g_free(key); |
Luiz Capitulino | 4d76d2b | 2009-08-28 15:27:09 -0300 | [diff] [blame] | 3446 | key = NULL; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 3447 | } |
| 3448 | /* check that all arguments were parsed */ |
blueswir1 | cd39008 | 2008-11-16 13:53:32 +0000 | [diff] [blame] | 3449 | while (qemu_isspace(*p)) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 3450 | p++; |
| 3451 | if (*p != '\0') { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 3452 | monitor_printf(mon, "%s: extraneous characters at the end of line\n", |
Bandan Das | ae50212 | 2015-06-03 18:38:08 -0400 | [diff] [blame] | 3453 | cmd->name); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 3454 | goto fail; |
| 3455 | } |
| 3456 | |
Bandan Das | ae50212 | 2015-06-03 18:38:08 -0400 | [diff] [blame] | 3457 | return qdict; |
Gerd Hoffmann | ac7531e | 2009-08-14 10:36:06 +0200 | [diff] [blame] | 3458 | |
Luiz Capitulino | 55f81d9 | 2009-08-28 15:27:22 -0300 | [diff] [blame] | 3459 | fail: |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 3460 | qobject_unref(qdict); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 3461 | g_free(key); |
Luiz Capitulino | 55f81d9 | 2009-08-28 15:27:22 -0300 | [diff] [blame] | 3462 | return NULL; |
| 3463 | } |
| 3464 | |
Markus Armbruster | 7ef6cf6 | 2015-03-06 19:12:36 +0100 | [diff] [blame] | 3465 | static void handle_hmp_command(Monitor *mon, const char *cmdline) |
Luiz Capitulino | 55f81d9 | 2009-08-28 15:27:22 -0300 | [diff] [blame] | 3466 | { |
Luiz Capitulino | 55f81d9 | 2009-08-28 15:27:22 -0300 | [diff] [blame] | 3467 | QDict *qdict; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3468 | const mon_cmd_t *cmd; |
Collin Walling | 317c52c | 2018-05-07 10:30:54 -0400 | [diff] [blame] | 3469 | const char *cmd_start = cmdline; |
Luiz Capitulino | 55f81d9 | 2009-08-28 15:27:22 -0300 | [diff] [blame] | 3470 | |
Stefan Hajnoczi | 79cad8b | 2017-06-05 11:42:15 +0100 | [diff] [blame] | 3471 | trace_handle_hmp_command(mon, cmdline); |
| 3472 | |
Dr. David Alan Gilbert | 250b819 | 2017-08-17 11:42:16 +0100 | [diff] [blame] | 3473 | cmd = monitor_parse_command(mon, cmdline, &cmdline, mon->cmd_table); |
Bandan Das | ae50212 | 2015-06-03 18:38:08 -0400 | [diff] [blame] | 3474 | if (!cmd) { |
| 3475 | return; |
Luiz Capitulino | 55f81d9 | 2009-08-28 15:27:22 -0300 | [diff] [blame] | 3476 | } |
| 3477 | |
Bandan Das | ae50212 | 2015-06-03 18:38:08 -0400 | [diff] [blame] | 3478 | qdict = monitor_parse_arguments(mon, &cmdline, cmd); |
| 3479 | if (!qdict) { |
Collin Walling | 317c52c | 2018-05-07 10:30:54 -0400 | [diff] [blame] | 3480 | while (cmdline > cmd_start && qemu_isspace(cmdline[-1])) { |
| 3481 | cmdline--; |
| 3482 | } |
| 3483 | monitor_printf(mon, "Try \"help %.*s\" for more information\n", |
| 3484 | (int)(cmdline - cmd_start), cmd_start); |
Bandan Das | ae50212 | 2015-06-03 18:38:08 -0400 | [diff] [blame] | 3485 | return; |
| 3486 | } |
| 3487 | |
Marc-André Lureau | 2b9e357 | 2016-09-12 13:19:06 +0400 | [diff] [blame] | 3488 | cmd->cmd(mon, qdict); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 3489 | qobject_unref(qdict); |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 3490 | } |
| 3491 | |
Wenchao Xia | cd5c6bb | 2013-08-27 20:38:13 +0800 | [diff] [blame] | 3492 | static void cmd_completion(Monitor *mon, const char *name, const char *list) |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 3493 | { |
| 3494 | const char *p, *pstart; |
| 3495 | char cmd[128]; |
| 3496 | int len; |
| 3497 | |
| 3498 | p = list; |
| 3499 | for(;;) { |
| 3500 | pstart = p; |
Keno Fischer | 5c99fa3 | 2018-06-29 12:32:10 +0200 | [diff] [blame] | 3501 | p = qemu_strchrnul(p, '|'); |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 3502 | len = p - pstart; |
| 3503 | if (len > sizeof(cmd) - 2) |
| 3504 | len = sizeof(cmd) - 2; |
| 3505 | memcpy(cmd, pstart, len); |
| 3506 | cmd[len] = '\0'; |
| 3507 | if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) { |
Wenchao Xia | cd5c6bb | 2013-08-27 20:38:13 +0800 | [diff] [blame] | 3508 | readline_add_completion(mon->rs, cmd); |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 3509 | } |
| 3510 | if (*p == '\0') |
| 3511 | break; |
| 3512 | p++; |
| 3513 | } |
| 3514 | } |
| 3515 | |
Wenchao Xia | cb8f68b | 2013-08-27 20:38:14 +0800 | [diff] [blame] | 3516 | static void file_completion(Monitor *mon, const char *input) |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 3517 | { |
| 3518 | DIR *ffs; |
| 3519 | struct dirent *d; |
| 3520 | char path[1024]; |
| 3521 | char file[1024], file_prefix[1024]; |
| 3522 | int input_path_len; |
| 3523 | const char *p; |
| 3524 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3525 | p = strrchr(input, '/'); |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 3526 | if (!p) { |
| 3527 | input_path_len = 0; |
| 3528 | pstrcpy(file_prefix, sizeof(file_prefix), input); |
blueswir1 | 363a37d | 2008-08-21 17:58:08 +0000 | [diff] [blame] | 3529 | pstrcpy(path, sizeof(path), "."); |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 3530 | } else { |
| 3531 | input_path_len = p - input + 1; |
| 3532 | memcpy(path, input, input_path_len); |
| 3533 | if (input_path_len > sizeof(path) - 1) |
| 3534 | input_path_len = sizeof(path) - 1; |
| 3535 | path[input_path_len] = '\0'; |
| 3536 | pstrcpy(file_prefix, sizeof(file_prefix), p + 1); |
| 3537 | } |
Bandan Das | 19f2db5 | 2015-06-03 18:38:07 -0400 | [diff] [blame] | 3538 | |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 3539 | ffs = opendir(path); |
| 3540 | if (!ffs) |
| 3541 | return; |
| 3542 | for(;;) { |
| 3543 | struct stat sb; |
| 3544 | d = readdir(ffs); |
| 3545 | if (!d) |
| 3546 | break; |
Kusanagi Kouichi | 46c7fc1 | 2010-10-20 18:00:01 +0900 | [diff] [blame] | 3547 | |
| 3548 | if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) { |
| 3549 | continue; |
| 3550 | } |
| 3551 | |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 3552 | if (strstart(d->d_name, file_prefix, NULL)) { |
| 3553 | memcpy(file, input, input_path_len); |
blueswir1 | 363a37d | 2008-08-21 17:58:08 +0000 | [diff] [blame] | 3554 | if (input_path_len < sizeof(file)) |
| 3555 | pstrcpy(file + input_path_len, sizeof(file) - input_path_len, |
| 3556 | d->d_name); |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 3557 | /* stat the file to find out if it's a directory. |
| 3558 | * In that case add a slash to speed up typing long paths |
| 3559 | */ |
Markus Armbruster | c951d9a | 2011-11-16 15:43:47 +0100 | [diff] [blame] | 3560 | if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) { |
blueswir1 | 363a37d | 2008-08-21 17:58:08 +0000 | [diff] [blame] | 3561 | pstrcat(file, sizeof(file), "/"); |
Markus Armbruster | c951d9a | 2011-11-16 15:43:47 +0100 | [diff] [blame] | 3562 | } |
Wenchao Xia | cb8f68b | 2013-08-27 20:38:14 +0800 | [diff] [blame] | 3563 | readline_add_completion(mon->rs, file); |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 3564 | } |
| 3565 | } |
| 3566 | closedir(ffs); |
| 3567 | } |
| 3568 | |
Luiz Capitulino | 4d76d2b | 2009-08-28 15:27:09 -0300 | [diff] [blame] | 3569 | static const char *next_arg_type(const char *typestr) |
| 3570 | { |
| 3571 | const char *p = strchr(typestr, ':'); |
| 3572 | return (p != NULL ? ++p : typestr); |
| 3573 | } |
| 3574 | |
Hani Benhabiles | 40d1939 | 2014-05-07 23:41:30 +0100 | [diff] [blame] | 3575 | static void add_completion_option(ReadLineState *rs, const char *str, |
| 3576 | const char *option) |
| 3577 | { |
| 3578 | if (!str || !option) { |
| 3579 | return; |
| 3580 | } |
| 3581 | if (!strncmp(option, str, strlen(str))) { |
| 3582 | readline_add_completion(rs, option); |
| 3583 | } |
| 3584 | } |
| 3585 | |
Hani Benhabiles | 13e315d | 2014-05-07 23:41:29 +0100 | [diff] [blame] | 3586 | void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str) |
| 3587 | { |
| 3588 | size_t len; |
| 3589 | ChardevBackendInfoList *list, *start; |
| 3590 | |
| 3591 | if (nb_args != 2) { |
| 3592 | return; |
| 3593 | } |
| 3594 | len = strlen(str); |
| 3595 | readline_set_completion_index(rs, len); |
| 3596 | |
| 3597 | start = list = qmp_query_chardev_backends(NULL); |
| 3598 | while (list) { |
| 3599 | const char *chr_name = list->value->name; |
| 3600 | |
| 3601 | if (!strncmp(chr_name, str, len)) { |
| 3602 | readline_add_completion(rs, chr_name); |
| 3603 | } |
| 3604 | list = list->next; |
| 3605 | } |
| 3606 | qapi_free_ChardevBackendInfoList(start); |
| 3607 | } |
| 3608 | |
Hani Benhabiles | b162b49 | 2014-05-07 23:41:31 +0100 | [diff] [blame] | 3609 | void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str) |
| 3610 | { |
| 3611 | size_t len; |
| 3612 | int i; |
| 3613 | |
| 3614 | if (nb_args != 2) { |
| 3615 | return; |
| 3616 | } |
| 3617 | len = strlen(str); |
| 3618 | readline_set_completion_index(rs, len); |
Markus Armbruster | 1c236ba | 2017-08-24 10:46:06 +0200 | [diff] [blame] | 3619 | for (i = 0; i < NET_CLIENT_DRIVER__MAX; i++) { |
Markus Armbruster | 977c736 | 2017-08-24 10:46:08 +0200 | [diff] [blame] | 3620 | add_completion_option(rs, str, NetClientDriver_str(i)); |
Hani Benhabiles | b162b49 | 2014-05-07 23:41:31 +0100 | [diff] [blame] | 3621 | } |
| 3622 | } |
| 3623 | |
Hani Benhabiles | 2da1b3a | 2014-04-13 16:25:07 +0100 | [diff] [blame] | 3624 | void device_add_completion(ReadLineState *rs, int nb_args, const char *str) |
Hani Benhabiles | 992d3e6 | 2014-02-06 23:30:11 +0100 | [diff] [blame] | 3625 | { |
| 3626 | GSList *list, *elt; |
| 3627 | size_t len; |
| 3628 | |
Hani Benhabiles | 2da1b3a | 2014-04-13 16:25:07 +0100 | [diff] [blame] | 3629 | if (nb_args != 2) { |
| 3630 | return; |
| 3631 | } |
| 3632 | |
Hani Benhabiles | 992d3e6 | 2014-02-06 23:30:11 +0100 | [diff] [blame] | 3633 | len = strlen(str); |
| 3634 | readline_set_completion_index(rs, len); |
| 3635 | list = elt = object_class_get_list(TYPE_DEVICE, false); |
| 3636 | while (elt) { |
| 3637 | const char *name; |
| 3638 | DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data, |
| 3639 | TYPE_DEVICE); |
| 3640 | name = object_class_get_name(OBJECT_CLASS(dc)); |
Hani Benhabiles | 2da1b3a | 2014-04-13 16:25:07 +0100 | [diff] [blame] | 3641 | |
Eduardo Habkost | e90f2a8 | 2017-05-03 17:35:44 -0300 | [diff] [blame] | 3642 | if (dc->user_creatable |
Hani Benhabiles | 2da1b3a | 2014-04-13 16:25:07 +0100 | [diff] [blame] | 3643 | && !strncmp(name, str, len)) { |
Hani Benhabiles | 992d3e6 | 2014-02-06 23:30:11 +0100 | [diff] [blame] | 3644 | readline_add_completion(rs, name); |
| 3645 | } |
| 3646 | elt = elt->next; |
| 3647 | } |
| 3648 | g_slist_free(list); |
| 3649 | } |
| 3650 | |
Hani Benhabiles | bfa40f7 | 2014-04-13 16:25:06 +0100 | [diff] [blame] | 3651 | void object_add_completion(ReadLineState *rs, int nb_args, const char *str) |
Hani Benhabiles | 1094fd3 | 2014-02-06 23:30:13 +0100 | [diff] [blame] | 3652 | { |
| 3653 | GSList *list, *elt; |
| 3654 | size_t len; |
| 3655 | |
Hani Benhabiles | bfa40f7 | 2014-04-13 16:25:06 +0100 | [diff] [blame] | 3656 | if (nb_args != 2) { |
| 3657 | return; |
| 3658 | } |
| 3659 | |
Hani Benhabiles | 1094fd3 | 2014-02-06 23:30:13 +0100 | [diff] [blame] | 3660 | len = strlen(str); |
| 3661 | readline_set_completion_index(rs, len); |
| 3662 | list = elt = object_class_get_list(TYPE_USER_CREATABLE, false); |
| 3663 | while (elt) { |
| 3664 | const char *name; |
| 3665 | |
| 3666 | name = object_class_get_name(OBJECT_CLASS(elt->data)); |
| 3667 | if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) { |
| 3668 | readline_add_completion(rs, name); |
| 3669 | } |
| 3670 | elt = elt->next; |
| 3671 | } |
| 3672 | g_slist_free(list); |
| 3673 | } |
| 3674 | |
Zhu Guihua | 6a1fa9f | 2014-10-21 19:46:05 +0800 | [diff] [blame] | 3675 | static void peripheral_device_del_completion(ReadLineState *rs, |
| 3676 | const char *str, size_t len) |
| 3677 | { |
Marcel Apfelbaum | 4cae4d5 | 2014-11-26 13:50:01 +0200 | [diff] [blame] | 3678 | Object *peripheral = container_get(qdev_get_machine(), "/peripheral"); |
| 3679 | GSList *list, *item; |
Zhu Guihua | 6a1fa9f | 2014-10-21 19:46:05 +0800 | [diff] [blame] | 3680 | |
Marcel Apfelbaum | 4cae4d5 | 2014-11-26 13:50:01 +0200 | [diff] [blame] | 3681 | list = qdev_build_hotpluggable_device_list(peripheral); |
| 3682 | if (!list) { |
Zhu Guihua | 6a1fa9f | 2014-10-21 19:46:05 +0800 | [diff] [blame] | 3683 | return; |
| 3684 | } |
| 3685 | |
Zhu Guihua | 6a1fa9f | 2014-10-21 19:46:05 +0800 | [diff] [blame] | 3686 | for (item = list; item; item = g_slist_next(item)) { |
| 3687 | DeviceState *dev = item->data; |
| 3688 | |
| 3689 | if (dev->id && !strncmp(str, dev->id, len)) { |
| 3690 | readline_add_completion(rs, dev->id); |
| 3691 | } |
| 3692 | } |
| 3693 | |
| 3694 | g_slist_free(list); |
| 3695 | } |
| 3696 | |
Hani Benhabiles | 6297d9a | 2014-05-07 23:41:28 +0100 | [diff] [blame] | 3697 | void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str) |
| 3698 | { |
| 3699 | size_t len; |
| 3700 | ChardevInfoList *list, *start; |
| 3701 | |
| 3702 | if (nb_args != 2) { |
| 3703 | return; |
| 3704 | } |
| 3705 | len = strlen(str); |
| 3706 | readline_set_completion_index(rs, len); |
| 3707 | |
| 3708 | start = list = qmp_query_chardev(NULL); |
| 3709 | while (list) { |
| 3710 | ChardevInfo *chr = list->value; |
| 3711 | |
| 3712 | if (!strncmp(chr->label, str, len)) { |
| 3713 | readline_add_completion(rs, chr->label); |
| 3714 | } |
| 3715 | list = list->next; |
| 3716 | } |
| 3717 | qapi_free_ChardevInfoList(start); |
| 3718 | } |
| 3719 | |
Hani Benhabiles | 8e59777 | 2014-05-27 23:39:30 +0100 | [diff] [blame] | 3720 | static void ringbuf_completion(ReadLineState *rs, const char *str) |
| 3721 | { |
| 3722 | size_t len; |
| 3723 | ChardevInfoList *list, *start; |
| 3724 | |
| 3725 | len = strlen(str); |
| 3726 | readline_set_completion_index(rs, len); |
| 3727 | |
| 3728 | start = list = qmp_query_chardev(NULL); |
| 3729 | while (list) { |
| 3730 | ChardevInfo *chr_info = list->value; |
| 3731 | |
| 3732 | if (!strncmp(chr_info->label, str, len)) { |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 3733 | Chardev *chr = qemu_chr_find(chr_info->label); |
Marc-André Lureau | 777357d | 2016-12-07 18:39:10 +0300 | [diff] [blame] | 3734 | if (chr && CHARDEV_IS_RINGBUF(chr)) { |
Hani Benhabiles | 8e59777 | 2014-05-27 23:39:30 +0100 | [diff] [blame] | 3735 | readline_add_completion(rs, chr_info->label); |
| 3736 | } |
| 3737 | } |
| 3738 | list = list->next; |
| 3739 | } |
| 3740 | qapi_free_ChardevInfoList(start); |
| 3741 | } |
| 3742 | |
Hani Benhabiles | 8e59777 | 2014-05-27 23:39:30 +0100 | [diff] [blame] | 3743 | void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str) |
| 3744 | { |
| 3745 | if (nb_args != 2) { |
| 3746 | return; |
| 3747 | } |
| 3748 | ringbuf_completion(rs, str); |
| 3749 | } |
| 3750 | |
Hani Benhabiles | 2da1b3a | 2014-04-13 16:25:07 +0100 | [diff] [blame] | 3751 | void device_del_completion(ReadLineState *rs, int nb_args, const char *str) |
| 3752 | { |
| 3753 | size_t len; |
| 3754 | |
| 3755 | if (nb_args != 2) { |
| 3756 | return; |
| 3757 | } |
| 3758 | |
| 3759 | len = strlen(str); |
| 3760 | readline_set_completion_index(rs, len); |
Zhu Guihua | 6a1fa9f | 2014-10-21 19:46:05 +0800 | [diff] [blame] | 3761 | peripheral_device_del_completion(rs, str, len); |
Hani Benhabiles | 2da1b3a | 2014-04-13 16:25:07 +0100 | [diff] [blame] | 3762 | } |
| 3763 | |
Hani Benhabiles | bfa40f7 | 2014-04-13 16:25:06 +0100 | [diff] [blame] | 3764 | void object_del_completion(ReadLineState *rs, int nb_args, const char *str) |
Hani Benhabiles | b48fa07 | 2014-02-06 23:30:12 +0100 | [diff] [blame] | 3765 | { |
| 3766 | ObjectPropertyInfoList *list, *start; |
| 3767 | size_t len; |
| 3768 | |
Hani Benhabiles | bfa40f7 | 2014-04-13 16:25:06 +0100 | [diff] [blame] | 3769 | if (nb_args != 2) { |
| 3770 | return; |
| 3771 | } |
Hani Benhabiles | b48fa07 | 2014-02-06 23:30:12 +0100 | [diff] [blame] | 3772 | len = strlen(str); |
| 3773 | readline_set_completion_index(rs, len); |
| 3774 | |
| 3775 | start = list = qmp_qom_list("/objects", NULL); |
| 3776 | while (list) { |
| 3777 | ObjectPropertyInfo *info = list->value; |
| 3778 | |
| 3779 | if (!strncmp(info->type, "child<", 5) |
| 3780 | && !strncmp(info->name, str, len)) { |
| 3781 | readline_add_completion(rs, info->name); |
| 3782 | } |
| 3783 | list = list->next; |
| 3784 | } |
| 3785 | qapi_free_ObjectPropertyInfoList(start); |
| 3786 | } |
| 3787 | |
Hani Benhabiles | 29136cd | 2014-05-07 23:41:27 +0100 | [diff] [blame] | 3788 | void sendkey_completion(ReadLineState *rs, int nb_args, const char *str) |
| 3789 | { |
| 3790 | int i; |
| 3791 | char *sep; |
| 3792 | size_t len; |
| 3793 | |
| 3794 | if (nb_args != 2) { |
| 3795 | return; |
| 3796 | } |
| 3797 | sep = strrchr(str, '-'); |
| 3798 | if (sep) { |
| 3799 | str = sep + 1; |
| 3800 | } |
| 3801 | len = strlen(str); |
| 3802 | readline_set_completion_index(rs, len); |
Eric Blake | 7fb1cf1 | 2015-11-18 01:52:57 -0700 | [diff] [blame] | 3803 | for (i = 0; i < Q_KEY_CODE__MAX; i++) { |
Markus Armbruster | 977c736 | 2017-08-24 10:46:08 +0200 | [diff] [blame] | 3804 | if (!strncmp(str, QKeyCode_str(i), len)) { |
| 3805 | readline_add_completion(rs, QKeyCode_str(i)); |
Hani Benhabiles | 29136cd | 2014-05-07 23:41:27 +0100 | [diff] [blame] | 3806 | } |
| 3807 | } |
| 3808 | } |
| 3809 | |
Hani Benhabiles | 40d1939 | 2014-05-07 23:41:30 +0100 | [diff] [blame] | 3810 | void set_link_completion(ReadLineState *rs, int nb_args, const char *str) |
| 3811 | { |
| 3812 | size_t len; |
| 3813 | |
| 3814 | len = strlen(str); |
| 3815 | readline_set_completion_index(rs, len); |
| 3816 | if (nb_args == 2) { |
Jason Wang | eaed483 | 2015-04-23 14:21:38 +0800 | [diff] [blame] | 3817 | NetClientState *ncs[MAX_QUEUE_NUM]; |
Hani Benhabiles | 40d1939 | 2014-05-07 23:41:30 +0100 | [diff] [blame] | 3818 | int count, i; |
| 3819 | count = qemu_find_net_clients_except(NULL, ncs, |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 3820 | NET_CLIENT_DRIVER_NONE, |
Jason Wang | eaed483 | 2015-04-23 14:21:38 +0800 | [diff] [blame] | 3821 | MAX_QUEUE_NUM); |
Jason Wang | bcfa4d6 | 2015-04-23 14:21:39 +0800 | [diff] [blame] | 3822 | for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) { |
Hani Benhabiles | 40d1939 | 2014-05-07 23:41:30 +0100 | [diff] [blame] | 3823 | const char *name = ncs[i]->name; |
| 3824 | if (!strncmp(str, name, len)) { |
| 3825 | readline_add_completion(rs, name); |
| 3826 | } |
| 3827 | } |
| 3828 | } else if (nb_args == 3) { |
| 3829 | add_completion_option(rs, str, "on"); |
| 3830 | add_completion_option(rs, str, "off"); |
| 3831 | } |
| 3832 | } |
| 3833 | |
Hani Benhabiles | 11b389f | 2014-05-07 23:41:32 +0100 | [diff] [blame] | 3834 | void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str) |
| 3835 | { |
| 3836 | int len, count, i; |
Jason Wang | eaed483 | 2015-04-23 14:21:38 +0800 | [diff] [blame] | 3837 | NetClientState *ncs[MAX_QUEUE_NUM]; |
Hani Benhabiles | 11b389f | 2014-05-07 23:41:32 +0100 | [diff] [blame] | 3838 | |
| 3839 | if (nb_args != 2) { |
| 3840 | return; |
| 3841 | } |
| 3842 | |
| 3843 | len = strlen(str); |
| 3844 | readline_set_completion_index(rs, len); |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 3845 | count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC, |
Jason Wang | eaed483 | 2015-04-23 14:21:38 +0800 | [diff] [blame] | 3846 | MAX_QUEUE_NUM); |
Jason Wang | bcfa4d6 | 2015-04-23 14:21:39 +0800 | [diff] [blame] | 3847 | for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) { |
Hani Benhabiles | 11b389f | 2014-05-07 23:41:32 +0100 | [diff] [blame] | 3848 | QemuOpts *opts; |
| 3849 | const char *name = ncs[i]->name; |
| 3850 | if (strncmp(str, name, len)) { |
| 3851 | continue; |
| 3852 | } |
| 3853 | opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name); |
| 3854 | if (opts) { |
| 3855 | readline_add_completion(rs, name); |
| 3856 | } |
| 3857 | } |
| 3858 | } |
| 3859 | |
Lluís Vilanova | bd71211 | 2016-07-11 12:53:51 +0200 | [diff] [blame] | 3860 | void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str) |
| 3861 | { |
| 3862 | size_t len; |
| 3863 | |
| 3864 | len = strlen(str); |
| 3865 | readline_set_completion_index(rs, len); |
| 3866 | if (nb_args == 2) { |
Daniel P. Berrange | 0d4e995 | 2016-10-04 14:35:43 +0100 | [diff] [blame] | 3867 | TraceEventIter iter; |
| 3868 | TraceEvent *ev; |
| 3869 | char *pattern = g_strdup_printf("%s*", str); |
| 3870 | trace_event_iter_init(&iter, pattern); |
| 3871 | while ((ev = trace_event_iter_next(&iter)) != NULL) { |
| 3872 | readline_add_completion(rs, trace_event_get_name(ev)); |
Lluís Vilanova | bd71211 | 2016-07-11 12:53:51 +0200 | [diff] [blame] | 3873 | } |
Daniel P. Berrange | 0d4e995 | 2016-10-04 14:35:43 +0100 | [diff] [blame] | 3874 | g_free(pattern); |
Lluís Vilanova | bd71211 | 2016-07-11 12:53:51 +0200 | [diff] [blame] | 3875 | } |
| 3876 | } |
| 3877 | |
Dr. David Alan Gilbert | 987bd27 | 2015-08-14 11:27:43 +0100 | [diff] [blame] | 3878 | void trace_event_completion(ReadLineState *rs, int nb_args, const char *str) |
| 3879 | { |
| 3880 | size_t len; |
| 3881 | |
| 3882 | len = strlen(str); |
| 3883 | readline_set_completion_index(rs, len); |
| 3884 | if (nb_args == 2) { |
Daniel P. Berrange | 0d4e995 | 2016-10-04 14:35:43 +0100 | [diff] [blame] | 3885 | TraceEventIter iter; |
| 3886 | TraceEvent *ev; |
| 3887 | char *pattern = g_strdup_printf("%s*", str); |
| 3888 | trace_event_iter_init(&iter, pattern); |
| 3889 | while ((ev = trace_event_iter_next(&iter)) != NULL) { |
| 3890 | readline_add_completion(rs, trace_event_get_name(ev)); |
Dr. David Alan Gilbert | 987bd27 | 2015-08-14 11:27:43 +0100 | [diff] [blame] | 3891 | } |
Daniel P. Berrange | 0d4e995 | 2016-10-04 14:35:43 +0100 | [diff] [blame] | 3892 | g_free(pattern); |
Dr. David Alan Gilbert | 987bd27 | 2015-08-14 11:27:43 +0100 | [diff] [blame] | 3893 | } else if (nb_args == 3) { |
| 3894 | add_completion_option(rs, str, "on"); |
| 3895 | add_completion_option(rs, str, "off"); |
| 3896 | } |
| 3897 | } |
| 3898 | |
Hani Benhabiles | d0ece34 | 2014-05-27 23:39:31 +0100 | [diff] [blame] | 3899 | void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str) |
| 3900 | { |
Hani Benhabiles | 4bb08af | 2014-07-29 23:22:40 +0100 | [diff] [blame] | 3901 | int i; |
| 3902 | |
Hani Benhabiles | d0ece34 | 2014-05-27 23:39:31 +0100 | [diff] [blame] | 3903 | if (nb_args != 2) { |
| 3904 | return; |
| 3905 | } |
| 3906 | readline_set_completion_index(rs, strlen(str)); |
Michal Privoznik | 14d53b4 | 2017-09-07 10:05:24 +0200 | [diff] [blame] | 3907 | for (i = 0; i < WATCHDOG_ACTION__MAX; i++) { |
| 3908 | add_completion_option(rs, str, WatchdogAction_str(i)); |
Hani Benhabiles | 4bb08af | 2014-07-29 23:22:40 +0100 | [diff] [blame] | 3909 | } |
Hani Benhabiles | d0ece34 | 2014-05-27 23:39:31 +0100 | [diff] [blame] | 3910 | } |
| 3911 | |
Hani Benhabiles | c68a040 | 2014-05-27 23:39:32 +0100 | [diff] [blame] | 3912 | void migrate_set_capability_completion(ReadLineState *rs, int nb_args, |
| 3913 | const char *str) |
| 3914 | { |
| 3915 | size_t len; |
| 3916 | |
| 3917 | len = strlen(str); |
| 3918 | readline_set_completion_index(rs, len); |
| 3919 | if (nb_args == 2) { |
| 3920 | int i; |
Eric Blake | 7fb1cf1 | 2015-11-18 01:52:57 -0700 | [diff] [blame] | 3921 | for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) { |
Markus Armbruster | 977c736 | 2017-08-24 10:46:08 +0200 | [diff] [blame] | 3922 | const char *name = MigrationCapability_str(i); |
Hani Benhabiles | c68a040 | 2014-05-27 23:39:32 +0100 | [diff] [blame] | 3923 | if (!strncmp(str, name, len)) { |
| 3924 | readline_add_completion(rs, name); |
| 3925 | } |
| 3926 | } |
| 3927 | } else if (nb_args == 3) { |
| 3928 | add_completion_option(rs, str, "on"); |
| 3929 | add_completion_option(rs, str, "off"); |
| 3930 | } |
| 3931 | } |
| 3932 | |
Liang Li | 50e9a62 | 2015-03-23 16:32:29 +0800 | [diff] [blame] | 3933 | void migrate_set_parameter_completion(ReadLineState *rs, int nb_args, |
| 3934 | const char *str) |
| 3935 | { |
| 3936 | size_t len; |
| 3937 | |
| 3938 | len = strlen(str); |
| 3939 | readline_set_completion_index(rs, len); |
| 3940 | if (nb_args == 2) { |
| 3941 | int i; |
Eric Blake | 7fb1cf1 | 2015-11-18 01:52:57 -0700 | [diff] [blame] | 3942 | for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) { |
Markus Armbruster | 977c736 | 2017-08-24 10:46:08 +0200 | [diff] [blame] | 3943 | const char *name = MigrationParameter_str(i); |
Liang Li | 50e9a62 | 2015-03-23 16:32:29 +0800 | [diff] [blame] | 3944 | if (!strncmp(str, name, len)) { |
| 3945 | readline_add_completion(rs, name); |
| 3946 | } |
| 3947 | } |
| 3948 | } |
| 3949 | } |
| 3950 | |
Hani Benhabiles | b21631f | 2014-05-27 23:39:37 +0100 | [diff] [blame] | 3951 | static void vm_completion(ReadLineState *rs, const char *str) |
| 3952 | { |
| 3953 | size_t len; |
Kevin Wolf | 7c8eece | 2016-03-22 18:58:50 +0100 | [diff] [blame] | 3954 | BlockDriverState *bs; |
Kevin Wolf | 88be7b4 | 2016-05-20 18:49:07 +0200 | [diff] [blame] | 3955 | BdrvNextIterator it; |
Hani Benhabiles | b21631f | 2014-05-27 23:39:37 +0100 | [diff] [blame] | 3956 | |
| 3957 | len = strlen(str); |
| 3958 | readline_set_completion_index(rs, len); |
Kevin Wolf | 7c8eece | 2016-03-22 18:58:50 +0100 | [diff] [blame] | 3959 | |
Kevin Wolf | 88be7b4 | 2016-05-20 18:49:07 +0200 | [diff] [blame] | 3960 | for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) { |
Hani Benhabiles | b21631f | 2014-05-27 23:39:37 +0100 | [diff] [blame] | 3961 | SnapshotInfoList *snapshots, *snapshot; |
Denis V. Lunev | 6bf1faa | 2015-11-04 20:19:42 +0300 | [diff] [blame] | 3962 | AioContext *ctx = bdrv_get_aio_context(bs); |
| 3963 | bool ok = false; |
Hani Benhabiles | b21631f | 2014-05-27 23:39:37 +0100 | [diff] [blame] | 3964 | |
Denis V. Lunev | 6bf1faa | 2015-11-04 20:19:42 +0300 | [diff] [blame] | 3965 | aio_context_acquire(ctx); |
| 3966 | if (bdrv_can_snapshot(bs)) { |
| 3967 | ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0; |
| 3968 | } |
| 3969 | aio_context_release(ctx); |
| 3970 | if (!ok) { |
Hani Benhabiles | b21631f | 2014-05-27 23:39:37 +0100 | [diff] [blame] | 3971 | continue; |
| 3972 | } |
Denis V. Lunev | 6bf1faa | 2015-11-04 20:19:42 +0300 | [diff] [blame] | 3973 | |
Hani Benhabiles | b21631f | 2014-05-27 23:39:37 +0100 | [diff] [blame] | 3974 | snapshot = snapshots; |
| 3975 | while (snapshot) { |
| 3976 | char *completion = snapshot->value->name; |
| 3977 | if (!strncmp(str, completion, len)) { |
| 3978 | readline_add_completion(rs, completion); |
| 3979 | } |
| 3980 | completion = snapshot->value->id; |
| 3981 | if (!strncmp(str, completion, len)) { |
| 3982 | readline_add_completion(rs, completion); |
| 3983 | } |
| 3984 | snapshot = snapshot->next; |
| 3985 | } |
| 3986 | qapi_free_SnapshotInfoList(snapshots); |
| 3987 | } |
| 3988 | |
| 3989 | } |
| 3990 | |
| 3991 | void delvm_completion(ReadLineState *rs, int nb_args, const char *str) |
| 3992 | { |
| 3993 | if (nb_args == 2) { |
| 3994 | vm_completion(rs, str); |
| 3995 | } |
| 3996 | } |
| 3997 | |
| 3998 | void loadvm_completion(ReadLineState *rs, int nb_args, const char *str) |
| 3999 | { |
| 4000 | if (nb_args == 2) { |
| 4001 | vm_completion(rs, str); |
| 4002 | } |
| 4003 | } |
| 4004 | |
Wenchao Xia | c35b640 | 2013-08-27 20:38:24 +0800 | [diff] [blame] | 4005 | static void monitor_find_completion_by_table(Monitor *mon, |
| 4006 | const mon_cmd_t *cmd_table, |
| 4007 | char **args, |
| 4008 | int nb_args) |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 4009 | { |
| 4010 | const char *cmdname; |
Wenchao Xia | c35b640 | 2013-08-27 20:38:24 +0800 | [diff] [blame] | 4011 | int i; |
Dr. David Alan Gilbert | bf67f1c | 2018-02-13 12:51:43 +0000 | [diff] [blame] | 4012 | const char *ptype, *old_ptype, *str, *name; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 4013 | const mon_cmd_t *cmd; |
Max Reitz | da27a00 | 2016-03-16 19:54:29 +0100 | [diff] [blame] | 4014 | BlockBackend *blk = NULL; |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 4015 | |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 4016 | if (nb_args <= 1) { |
| 4017 | /* command completion */ |
| 4018 | if (nb_args == 0) |
| 4019 | cmdname = ""; |
| 4020 | else |
| 4021 | cmdname = args[0]; |
Wenchao Xia | d2674b2 | 2013-08-27 20:38:16 +0800 | [diff] [blame] | 4022 | readline_set_completion_index(mon->rs, strlen(cmdname)); |
Wenchao Xia | c35b640 | 2013-08-27 20:38:24 +0800 | [diff] [blame] | 4023 | for (cmd = cmd_table; cmd->name != NULL; cmd++) { |
Dr. David Alan Gilbert | 6d9f783 | 2018-06-20 16:39:43 +0100 | [diff] [blame] | 4024 | if (!runstate_check(RUN_STATE_PRECONFIG) || |
| 4025 | cmd_can_preconfig(cmd)) { |
| 4026 | cmd_completion(mon, cmdname, cmd->name); |
| 4027 | } |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 4028 | } |
| 4029 | } else { |
| 4030 | /* find the command */ |
Wenchao Xia | c35b640 | 2013-08-27 20:38:24 +0800 | [diff] [blame] | 4031 | for (cmd = cmd_table; cmd->name != NULL; cmd++) { |
Dr. David Alan Gilbert | 6d9f783 | 2018-06-20 16:39:43 +0100 | [diff] [blame] | 4032 | if (compare_cmd(args[0], cmd->name) && |
| 4033 | (!runstate_check(RUN_STATE_PRECONFIG) || |
| 4034 | cmd_can_preconfig(cmd))) { |
Jan Kiszka | 03a6348 | 2010-06-16 00:38:33 +0200 | [diff] [blame] | 4035 | break; |
| 4036 | } |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 4037 | } |
Jan Kiszka | 03a6348 | 2010-06-16 00:38:33 +0200 | [diff] [blame] | 4038 | if (!cmd->name) { |
Wenchao Xia | c35b640 | 2013-08-27 20:38:24 +0800 | [diff] [blame] | 4039 | return; |
Jan Kiszka | 03a6348 | 2010-06-16 00:38:33 +0200 | [diff] [blame] | 4040 | } |
| 4041 | |
Wenchao Xia | d903a77 | 2013-08-27 20:38:25 +0800 | [diff] [blame] | 4042 | if (cmd->sub_table) { |
| 4043 | /* do the job again */ |
Stefan Weil | e7ae771 | 2015-03-08 19:30:01 +0100 | [diff] [blame] | 4044 | monitor_find_completion_by_table(mon, cmd->sub_table, |
| 4045 | &args[1], nb_args - 1); |
| 4046 | return; |
Wenchao Xia | d903a77 | 2013-08-27 20:38:25 +0800 | [diff] [blame] | 4047 | } |
Hani Benhabiles | bfa40f7 | 2014-04-13 16:25:06 +0100 | [diff] [blame] | 4048 | if (cmd->command_completion) { |
Stefan Weil | e7ae771 | 2015-03-08 19:30:01 +0100 | [diff] [blame] | 4049 | cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]); |
| 4050 | return; |
Hani Benhabiles | bfa40f7 | 2014-04-13 16:25:06 +0100 | [diff] [blame] | 4051 | } |
Wenchao Xia | d903a77 | 2013-08-27 20:38:25 +0800 | [diff] [blame] | 4052 | |
Luiz Capitulino | 4d76d2b | 2009-08-28 15:27:09 -0300 | [diff] [blame] | 4053 | ptype = next_arg_type(cmd->args_type); |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 4054 | for(i = 0; i < nb_args - 2; i++) { |
| 4055 | if (*ptype != '\0') { |
Luiz Capitulino | 4d76d2b | 2009-08-28 15:27:09 -0300 | [diff] [blame] | 4056 | ptype = next_arg_type(ptype); |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 4057 | while (*ptype == '?') |
Luiz Capitulino | 4d76d2b | 2009-08-28 15:27:09 -0300 | [diff] [blame] | 4058 | ptype = next_arg_type(ptype); |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 4059 | } |
| 4060 | } |
| 4061 | str = args[nb_args - 1]; |
Dr. David Alan Gilbert | bf67f1c | 2018-02-13 12:51:43 +0000 | [diff] [blame] | 4062 | old_ptype = NULL; |
| 4063 | while (*ptype == '-' && old_ptype != ptype) { |
| 4064 | old_ptype = ptype; |
Jan Kiszka | 3b6dbf2 | 2010-06-16 00:38:34 +0200 | [diff] [blame] | 4065 | ptype = next_arg_type(ptype); |
Blue Swirl | 2a1704a | 2009-08-23 20:10:28 +0000 | [diff] [blame] | 4066 | } |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 4067 | switch(*ptype) { |
| 4068 | case 'F': |
| 4069 | /* file completion */ |
Wenchao Xia | d2674b2 | 2013-08-27 20:38:16 +0800 | [diff] [blame] | 4070 | readline_set_completion_index(mon->rs, strlen(str)); |
Wenchao Xia | cb8f68b | 2013-08-27 20:38:14 +0800 | [diff] [blame] | 4071 | file_completion(mon, str); |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 4072 | break; |
| 4073 | case 'B': |
| 4074 | /* block device name completion */ |
Wenchao Xia | 599a926 | 2013-08-27 20:38:15 +0800 | [diff] [blame] | 4075 | readline_set_completion_index(mon->rs, strlen(str)); |
Max Reitz | da27a00 | 2016-03-16 19:54:29 +0100 | [diff] [blame] | 4076 | while ((blk = blk_next(blk)) != NULL) { |
| 4077 | name = blk_name(blk); |
Markus Armbruster | fea68bb | 2014-10-07 13:59:10 +0200 | [diff] [blame] | 4078 | if (str[0] == '\0' || |
| 4079 | !strncmp(name, str, strlen(str))) { |
| 4080 | readline_add_completion(mon->rs, name); |
| 4081 | } |
| 4082 | } |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 4083 | break; |
bellard | 7fe4848 | 2004-10-09 18:08:01 +0000 | [diff] [blame] | 4084 | case 's': |
Wenchao Xia | 129be00 | 2013-08-27 20:38:26 +0800 | [diff] [blame] | 4085 | case 'S': |
Hani Benhabiles | 29136cd | 2014-05-07 23:41:27 +0100 | [diff] [blame] | 4086 | if (!strcmp(cmd->name, "help|?")) { |
Wenchao Xia | 7ca0e06 | 2013-08-27 20:38:27 +0800 | [diff] [blame] | 4087 | monitor_find_completion_by_table(mon, cmd_table, |
| 4088 | &args[1], nb_args - 1); |
bellard | 7fe4848 | 2004-10-09 18:08:01 +0000 | [diff] [blame] | 4089 | } |
| 4090 | break; |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 4091 | default: |
| 4092 | break; |
| 4093 | } |
| 4094 | } |
Wenchao Xia | c35b640 | 2013-08-27 20:38:24 +0800 | [diff] [blame] | 4095 | } |
| 4096 | |
Stefan Hajnoczi | c60bf33 | 2013-11-14 11:54:14 +0100 | [diff] [blame] | 4097 | static void monitor_find_completion(void *opaque, |
Wenchao Xia | c35b640 | 2013-08-27 20:38:24 +0800 | [diff] [blame] | 4098 | const char *cmdline) |
| 4099 | { |
Stefan Hajnoczi | c60bf33 | 2013-11-14 11:54:14 +0100 | [diff] [blame] | 4100 | Monitor *mon = opaque; |
Wenchao Xia | c35b640 | 2013-08-27 20:38:24 +0800 | [diff] [blame] | 4101 | char *args[MAX_ARGS]; |
| 4102 | int nb_args, len; |
| 4103 | |
| 4104 | /* 1. parse the cmdline */ |
| 4105 | if (parse_cmdline(cmdline, &nb_args, args) < 0) { |
| 4106 | return; |
| 4107 | } |
Wenchao Xia | c35b640 | 2013-08-27 20:38:24 +0800 | [diff] [blame] | 4108 | |
| 4109 | /* if the line ends with a space, it means we want to complete the |
| 4110 | next arg */ |
| 4111 | len = strlen(cmdline); |
| 4112 | if (len > 0 && qemu_isspace(cmdline[len - 1])) { |
| 4113 | if (nb_args >= MAX_ARGS) { |
| 4114 | goto cleanup; |
| 4115 | } |
| 4116 | args[nb_args++] = g_strdup(""); |
| 4117 | } |
| 4118 | |
| 4119 | /* 2. auto complete according to args */ |
| 4120 | monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args); |
Jan Kiszka | 03a6348 | 2010-06-16 00:38:33 +0200 | [diff] [blame] | 4121 | |
| 4122 | cleanup: |
Wenchao Xia | dcc70cd | 2013-08-27 20:38:22 +0800 | [diff] [blame] | 4123 | free_cmdline_args(args, nb_args); |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 4124 | } |
| 4125 | |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 4126 | static int monitor_can_read(void *opaque) |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 4127 | { |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 4128 | Monitor *mon = opaque; |
| 4129 | |
Peter Xu | df152fb | 2018-03-09 16:59:55 +0800 | [diff] [blame] | 4130 | return !atomic_mb_read(&mon->suspend_cnt); |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 4131 | } |
| 4132 | |
Peter Xu | 546aa56 | 2018-03-09 16:59:54 +0800 | [diff] [blame] | 4133 | /* |
Markus Armbruster | 7cb2123 | 2018-07-03 10:53:52 +0200 | [diff] [blame] | 4134 | * Emit QMP response @rsp with ID @id to @mon. |
| 4135 | * Null @rsp can only happen for commands with QCO_NO_SUCCESS_RESP. |
| 4136 | * Nothing is emitted then. |
Peter Xu | 546aa56 | 2018-03-09 16:59:54 +0800 | [diff] [blame] | 4137 | */ |
Markus Armbruster | 7cb2123 | 2018-07-03 10:53:52 +0200 | [diff] [blame] | 4138 | static void monitor_qmp_respond(Monitor *mon, QDict *rsp, QObject *id) |
Peter Xu | 546aa56 | 2018-03-09 16:59:54 +0800 | [diff] [blame] | 4139 | { |
Peter Xu | 546aa56 | 2018-03-09 16:59:54 +0800 | [diff] [blame] | 4140 | if (rsp) { |
| 4141 | if (id) { |
Markus Armbruster | d43b169 | 2018-07-03 10:53:49 +0200 | [diff] [blame] | 4142 | qdict_put_obj(rsp, "id", qobject_ref(id)); |
Peter Xu | 546aa56 | 2018-03-09 16:59:54 +0800 | [diff] [blame] | 4143 | } |
| 4144 | |
Markus Armbruster | 65e3fe6 | 2018-07-03 10:53:50 +0200 | [diff] [blame] | 4145 | qmp_queue_response(mon, rsp); |
Peter Xu | 546aa56 | 2018-03-09 16:59:54 +0800 | [diff] [blame] | 4146 | } |
Peter Xu | 546aa56 | 2018-03-09 16:59:54 +0800 | [diff] [blame] | 4147 | } |
| 4148 | |
Markus Armbruster | b273145 | 2018-07-03 10:53:41 +0200 | [diff] [blame] | 4149 | static void monitor_qmp_dispatch(Monitor *mon, QObject *req, QObject *id) |
Luiz Capitulino | 5fa737a | 2009-11-26 22:59:01 -0200 | [diff] [blame] | 4150 | { |
Markus Armbruster | b273145 | 2018-07-03 10:53:41 +0200 | [diff] [blame] | 4151 | Monitor *old_mon; |
Markus Armbruster | d43b169 | 2018-07-03 10:53:49 +0200 | [diff] [blame] | 4152 | QDict *rsp; |
Markus Armbruster | 69240fe | 2018-07-03 10:53:43 +0200 | [diff] [blame] | 4153 | QDict *error; |
Stefan Hajnoczi | b097efc | 2017-06-05 11:42:16 +0100 | [diff] [blame] | 4154 | |
Peter Xu | 227a075 | 2018-03-09 16:59:49 +0800 | [diff] [blame] | 4155 | old_mon = cur_mon; |
| 4156 | cur_mon = mon; |
| 4157 | |
Markus Armbruster | 674ed72 | 2018-07-03 10:53:37 +0200 | [diff] [blame] | 4158 | rsp = qmp_dispatch(mon->qmp.commands, req, qmp_oob_enabled(mon)); |
Luiz Capitulino | 5fa737a | 2009-11-26 22:59:01 -0200 | [diff] [blame] | 4159 | |
Peter Xu | 227a075 | 2018-03-09 16:59:49 +0800 | [diff] [blame] | 4160 | cur_mon = old_mon; |
| 4161 | |
Markus Armbruster | 69240fe | 2018-07-03 10:53:43 +0200 | [diff] [blame] | 4162 | if (mon->qmp.commands == &qmp_cap_negotiation_commands) { |
Markus Armbruster | d43b169 | 2018-07-03 10:53:49 +0200 | [diff] [blame] | 4163 | error = qdict_get_qdict(rsp, "error"); |
Markus Armbruster | 69240fe | 2018-07-03 10:53:43 +0200 | [diff] [blame] | 4164 | if (error |
| 4165 | && !g_strcmp0(qdict_get_try_str(error, "class"), |
| 4166 | QapiErrorClass_str(ERROR_CLASS_COMMAND_NOT_FOUND))) { |
| 4167 | /* Provide a more useful error message */ |
| 4168 | qdict_del(error, "desc"); |
| 4169 | qdict_put_str(error, "desc", "Expecting capabilities negotiation" |
| 4170 | " with 'qmp_capabilities'"); |
| 4171 | } |
Peter Xu | 71da466 | 2018-03-09 16:59:57 +0800 | [diff] [blame] | 4172 | } |
Marc-André Lureau | 5c678ee | 2016-09-12 13:19:10 +0400 | [diff] [blame] | 4173 | |
Markus Armbruster | 7cb2123 | 2018-07-03 10:53:52 +0200 | [diff] [blame] | 4174 | monitor_qmp_respond(mon, rsp, id); |
| 4175 | qobject_unref(rsp); |
Luiz Capitulino | 5fa737a | 2009-11-26 22:59:01 -0200 | [diff] [blame] | 4176 | } |
| 4177 | |
Peter Xu | 71da466 | 2018-03-09 16:59:57 +0800 | [diff] [blame] | 4178 | /* |
Markus Armbruster | 774a6b6 | 2018-07-03 10:53:57 +0200 | [diff] [blame] | 4179 | * Pop a QMP request from a monitor request queue. |
| 4180 | * Return the request, or NULL all request queues are empty. |
Peter Xu | 71da466 | 2018-03-09 16:59:57 +0800 | [diff] [blame] | 4181 | * We are using round-robin fashion to pop the request, to avoid |
| 4182 | * processing commands only on a very busy monitor. To achieve that, |
| 4183 | * when we process one request on a specific monitor, we put that |
| 4184 | * monitor to the end of mon_list queue. |
| 4185 | */ |
Peter Xu | 40687eb | 2018-06-20 15:32:18 +0800 | [diff] [blame] | 4186 | static QMPRequest *monitor_qmp_requests_pop_any(void) |
Peter Xu | 71da466 | 2018-03-09 16:59:57 +0800 | [diff] [blame] | 4187 | { |
| 4188 | QMPRequest *req_obj = NULL; |
| 4189 | Monitor *mon; |
| 4190 | |
| 4191 | qemu_mutex_lock(&monitor_lock); |
| 4192 | |
| 4193 | QTAILQ_FOREACH(mon, &mon_list, entry) { |
| 4194 | qemu_mutex_lock(&mon->qmp.qmp_queue_lock); |
| 4195 | req_obj = g_queue_pop_head(mon->qmp.qmp_requests); |
| 4196 | qemu_mutex_unlock(&mon->qmp.qmp_queue_lock); |
| 4197 | if (req_obj) { |
| 4198 | break; |
| 4199 | } |
| 4200 | } |
| 4201 | |
| 4202 | if (req_obj) { |
| 4203 | /* |
| 4204 | * We found one request on the monitor. Degrade this monitor's |
| 4205 | * priority to lowest by re-inserting it to end of queue. |
| 4206 | */ |
| 4207 | QTAILQ_REMOVE(&mon_list, mon, entry); |
| 4208 | QTAILQ_INSERT_TAIL(&mon_list, mon, entry); |
| 4209 | } |
| 4210 | |
| 4211 | qemu_mutex_unlock(&monitor_lock); |
| 4212 | |
| 4213 | return req_obj; |
| 4214 | } |
| 4215 | |
| 4216 | static void monitor_qmp_bh_dispatcher(void *data) |
| 4217 | { |
Peter Xu | 40687eb | 2018-06-20 15:32:18 +0800 | [diff] [blame] | 4218 | QMPRequest *req_obj = monitor_qmp_requests_pop_any(); |
Markus Armbruster | 7cb2123 | 2018-07-03 10:53:52 +0200 | [diff] [blame] | 4219 | QDict *rsp; |
Peter Xu | 71da466 | 2018-03-09 16:59:57 +0800 | [diff] [blame] | 4220 | |
Markus Armbruster | b273145 | 2018-07-03 10:53:41 +0200 | [diff] [blame] | 4221 | if (!req_obj) { |
| 4222 | return; |
Peter Xu | 71da466 | 2018-03-09 16:59:57 +0800 | [diff] [blame] | 4223 | } |
Markus Armbruster | b273145 | 2018-07-03 10:53:41 +0200 | [diff] [blame] | 4224 | |
Markus Armbruster | 1cc3747 | 2018-07-03 10:53:44 +0200 | [diff] [blame] | 4225 | if (req_obj->req) { |
| 4226 | trace_monitor_qmp_cmd_in_band(qobject_get_try_str(req_obj->id) ?: ""); |
| 4227 | monitor_qmp_dispatch(req_obj->mon, req_obj->req, req_obj->id); |
| 4228 | } else { |
| 4229 | assert(req_obj->err); |
Markus Armbruster | 7cb2123 | 2018-07-03 10:53:52 +0200 | [diff] [blame] | 4230 | rsp = qmp_error_response(req_obj->err); |
Marc-André Lureau | 42eab8d | 2018-07-05 18:42:01 +0200 | [diff] [blame] | 4231 | req_obj->err = NULL; |
Markus Armbruster | 7cb2123 | 2018-07-03 10:53:52 +0200 | [diff] [blame] | 4232 | monitor_qmp_respond(req_obj->mon, rsp, NULL); |
| 4233 | qobject_unref(rsp); |
Markus Armbruster | 1cc3747 | 2018-07-03 10:53:44 +0200 | [diff] [blame] | 4234 | } |
| 4235 | |
Markus Armbruster | b273145 | 2018-07-03 10:53:41 +0200 | [diff] [blame] | 4236 | if (req_obj->need_resume) { |
| 4237 | /* Pairs with the monitor_suspend() in handle_qmp_command() */ |
| 4238 | monitor_resume(req_obj->mon); |
| 4239 | } |
| 4240 | qmp_request_free(req_obj); |
| 4241 | |
| 4242 | /* Reschedule instead of looping so the main loop stays responsive */ |
Markus Armbruster | cab5ad8 | 2018-07-03 10:53:46 +0200 | [diff] [blame] | 4243 | qemu_bh_schedule(qmp_dispatcher_bh); |
Peter Xu | 71da466 | 2018-03-09 16:59:57 +0800 | [diff] [blame] | 4244 | } |
| 4245 | |
Peter Xu | bf1e730 | 2018-03-09 16:59:59 +0800 | [diff] [blame] | 4246 | #define QMP_REQ_QUEUE_LEN_MAX (8) |
| 4247 | |
Peter Xu | 71da466 | 2018-03-09 16:59:57 +0800 | [diff] [blame] | 4248 | static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens) |
| 4249 | { |
| 4250 | QObject *req, *id = NULL; |
Markus Armbruster | 0fa39d0 | 2018-07-03 10:53:35 +0200 | [diff] [blame] | 4251 | QDict *qdict; |
Peter Xu | 71da466 | 2018-03-09 16:59:57 +0800 | [diff] [blame] | 4252 | MonitorQMP *mon_qmp = container_of(parser, MonitorQMP, parser); |
| 4253 | Monitor *mon = container_of(mon_qmp, Monitor, qmp); |
| 4254 | Error *err = NULL; |
| 4255 | QMPRequest *req_obj; |
| 4256 | |
| 4257 | req = json_parser_parse_err(tokens, NULL, &err); |
| 4258 | if (!req && !err) { |
| 4259 | /* json_parser_parse_err() sucks: can fail without setting @err */ |
| 4260 | error_setg(&err, QERR_JSON_PARSING); |
| 4261 | } |
Peter Xu | 71da466 | 2018-03-09 16:59:57 +0800 | [diff] [blame] | 4262 | |
Markus Armbruster | 0fa39d0 | 2018-07-03 10:53:35 +0200 | [diff] [blame] | 4263 | qdict = qobject_to(QDict, req); |
| 4264 | if (qdict) { |
| 4265 | id = qobject_ref(qdict_get(qdict, "id")); |
| 4266 | qdict_del(qdict, "id"); |
| 4267 | } /* else will fail qmp_dispatch() */ |
| 4268 | |
Markus Armbruster | 8720e63 | 2018-07-16 11:10:12 +0200 | [diff] [blame] | 4269 | if (req && trace_event_get_state_backends(TRACE_HANDLE_QMP_COMMAND)) { |
Markus Armbruster | 45434ba | 2018-07-03 10:53:39 +0200 | [diff] [blame] | 4270 | QString *req_json = qobject_to_json(req); |
| 4271 | trace_handle_qmp_command(mon, qstring_get_str(req_json)); |
| 4272 | qobject_unref(req_json); |
Peter Xu | 71da466 | 2018-03-09 16:59:57 +0800 | [diff] [blame] | 4273 | } |
| 4274 | |
Markus Armbruster | 69240fe | 2018-07-03 10:53:43 +0200 | [diff] [blame] | 4275 | if (qdict && qmp_is_oob(qdict)) { |
Markus Armbruster | 774a6b6 | 2018-07-03 10:53:57 +0200 | [diff] [blame] | 4276 | /* OOB commands are executed immediately */ |
Markus Armbruster | b273145 | 2018-07-03 10:53:41 +0200 | [diff] [blame] | 4277 | trace_monitor_qmp_cmd_out_of_band(qobject_get_try_str(id) |
| 4278 | ?: ""); |
| 4279 | monitor_qmp_dispatch(mon, req, id); |
Marc-André Lureau | cb9ec42 | 2018-08-09 13:44:16 +0200 | [diff] [blame] | 4280 | qobject_unref(req); |
| 4281 | qobject_unref(id); |
Markus Armbruster | b273145 | 2018-07-03 10:53:41 +0200 | [diff] [blame] | 4282 | return; |
Peter Xu | cf869d5 | 2018-03-10 20:38:05 -0600 | [diff] [blame] | 4283 | } |
| 4284 | |
Peter Xu | 71da466 | 2018-03-09 16:59:57 +0800 | [diff] [blame] | 4285 | req_obj = g_new0(QMPRequest, 1); |
| 4286 | req_obj->mon = mon; |
Markus Armbruster | 0fa39d0 | 2018-07-03 10:53:35 +0200 | [diff] [blame] | 4287 | req_obj->id = id; |
Peter Xu | 71da466 | 2018-03-09 16:59:57 +0800 | [diff] [blame] | 4288 | req_obj->req = req; |
Markus Armbruster | 1cc3747 | 2018-07-03 10:53:44 +0200 | [diff] [blame] | 4289 | req_obj->err = err; |
Peter Xu | 71da466 | 2018-03-09 16:59:57 +0800 | [diff] [blame] | 4290 | req_obj->need_resume = false; |
| 4291 | |
Peter Xu | bf1e730 | 2018-03-09 16:59:59 +0800 | [diff] [blame] | 4292 | /* Protect qmp_requests and fetching its length. */ |
| 4293 | qemu_mutex_lock(&mon->qmp.qmp_queue_lock); |
| 4294 | |
Peter Xu | 71da466 | 2018-03-09 16:59:57 +0800 | [diff] [blame] | 4295 | /* |
| 4296 | * If OOB is not enabled on the current monitor, we'll emulate the |
| 4297 | * old behavior that we won't process the current monitor any more |
| 4298 | * until it has responded. This helps make sure that as long as |
| 4299 | * OOB is not enabled, the server will never drop any command. |
| 4300 | */ |
| 4301 | if (!qmp_oob_enabled(mon)) { |
| 4302 | monitor_suspend(mon); |
| 4303 | req_obj->need_resume = true; |
Peter Xu | bf1e730 | 2018-03-09 16:59:59 +0800 | [diff] [blame] | 4304 | } else { |
| 4305 | /* Drop the request if queue is full. */ |
| 4306 | if (mon->qmp.qmp_requests->length >= QMP_REQ_QUEUE_LEN_MAX) { |
| 4307 | qemu_mutex_unlock(&mon->qmp.qmp_queue_lock); |
Markus Armbruster | d621cfe | 2018-07-03 10:53:30 +0200 | [diff] [blame] | 4308 | /* |
| 4309 | * FIXME @id's scope is just @mon, and broadcasting it is |
| 4310 | * wrong. If another monitor's client has a command with |
| 4311 | * the same ID in flight, the event will incorrectly claim |
| 4312 | * that command was dropped. |
| 4313 | */ |
Peter Xu | bf1e730 | 2018-03-09 16:59:59 +0800 | [diff] [blame] | 4314 | qapi_event_send_command_dropped(id, |
| 4315 | COMMAND_DROP_REASON_QUEUE_FULL, |
| 4316 | &error_abort); |
Peter Xu | 6d2d563 | 2018-03-26 14:38:55 +0800 | [diff] [blame] | 4317 | qmp_request_free(req_obj); |
Peter Xu | bf1e730 | 2018-03-09 16:59:59 +0800 | [diff] [blame] | 4318 | return; |
| 4319 | } |
Peter Xu | 71da466 | 2018-03-09 16:59:57 +0800 | [diff] [blame] | 4320 | } |
| 4321 | |
| 4322 | /* |
| 4323 | * Put the request to the end of queue so that requests will be |
| 4324 | * handled in time order. Ownership for req_obj, req, id, |
| 4325 | * etc. will be delivered to the handler side. |
| 4326 | */ |
Peter Xu | 71da466 | 2018-03-09 16:59:57 +0800 | [diff] [blame] | 4327 | g_queue_push_tail(mon->qmp.qmp_requests, req_obj); |
| 4328 | qemu_mutex_unlock(&mon->qmp.qmp_queue_lock); |
| 4329 | |
| 4330 | /* Kick the dispatcher routine */ |
Markus Armbruster | cab5ad8 | 2018-07-03 10:53:46 +0200 | [diff] [blame] | 4331 | qemu_bh_schedule(qmp_dispatcher_bh); |
Peter Xu | 71da466 | 2018-03-09 16:59:57 +0800 | [diff] [blame] | 4332 | } |
| 4333 | |
Markus Armbruster | c83fe23 | 2015-03-06 19:20:51 +0100 | [diff] [blame] | 4334 | static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size) |
Luiz Capitulino | 9b57c02 | 2009-11-26 22:58:58 -0200 | [diff] [blame] | 4335 | { |
Peter Xu | 227a075 | 2018-03-09 16:59:49 +0800 | [diff] [blame] | 4336 | Monitor *mon = opaque; |
Luiz Capitulino | 9b57c02 | 2009-11-26 22:58:58 -0200 | [diff] [blame] | 4337 | |
Peter Xu | 227a075 | 2018-03-09 16:59:49 +0800 | [diff] [blame] | 4338 | json_message_parser_feed(&mon->qmp.parser, (const char *) buf, size); |
Luiz Capitulino | 9b57c02 | 2009-11-26 22:58:58 -0200 | [diff] [blame] | 4339 | } |
| 4340 | |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 4341 | static void monitor_read(void *opaque, const uint8_t *buf, int size) |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 4342 | { |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 4343 | Monitor *old_mon = cur_mon; |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 4344 | int i; |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 4345 | |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 4346 | cur_mon = opaque; |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 4347 | |
aliguori | cde76ee | 2009-03-05 23:01:51 +0000 | [diff] [blame] | 4348 | if (cur_mon->rs) { |
| 4349 | for (i = 0; i < size; i++) |
| 4350 | readline_handle_byte(cur_mon->rs, buf[i]); |
| 4351 | } else { |
| 4352 | if (size == 0 || buf[size - 1] != 0) |
| 4353 | monitor_printf(cur_mon, "corrupted command\n"); |
| 4354 | else |
Markus Armbruster | 7ef6cf6 | 2015-03-06 19:12:36 +0100 | [diff] [blame] | 4355 | handle_hmp_command(cur_mon, (char *)buf); |
aliguori | cde76ee | 2009-03-05 23:01:51 +0000 | [diff] [blame] | 4356 | } |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 4357 | |
| 4358 | cur_mon = old_mon; |
| 4359 | } |
aliguori | d8f4460 | 2008-10-06 13:52:44 +0000 | [diff] [blame] | 4360 | |
Stefan Hajnoczi | c60bf33 | 2013-11-14 11:54:14 +0100 | [diff] [blame] | 4361 | static void monitor_command_cb(void *opaque, const char *cmdline, |
| 4362 | void *readline_opaque) |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 4363 | { |
Stefan Hajnoczi | c60bf33 | 2013-11-14 11:54:14 +0100 | [diff] [blame] | 4364 | Monitor *mon = opaque; |
| 4365 | |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 4366 | monitor_suspend(mon); |
Markus Armbruster | 7ef6cf6 | 2015-03-06 19:12:36 +0100 | [diff] [blame] | 4367 | handle_hmp_command(mon, cmdline); |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 4368 | monitor_resume(mon); |
aliguori | d8f4460 | 2008-10-06 13:52:44 +0000 | [diff] [blame] | 4369 | } |
| 4370 | |
aliguori | cde76ee | 2009-03-05 23:01:51 +0000 | [diff] [blame] | 4371 | int monitor_suspend(Monitor *mon) |
aliguori | d8f4460 | 2008-10-06 13:52:44 +0000 | [diff] [blame] | 4372 | { |
Peter Xu | e3e977d | 2018-03-09 16:59:56 +0800 | [diff] [blame] | 4373 | if (monitor_is_hmp_non_interactive(mon)) { |
aliguori | cde76ee | 2009-03-05 23:01:51 +0000 | [diff] [blame] | 4374 | return -ENOTTY; |
Peter Xu | e3e977d | 2018-03-09 16:59:56 +0800 | [diff] [blame] | 4375 | } |
| 4376 | |
Peter Xu | df152fb | 2018-03-09 16:59:55 +0800 | [diff] [blame] | 4377 | atomic_inc(&mon->suspend_cnt); |
Peter Xu | e3e977d | 2018-03-09 16:59:56 +0800 | [diff] [blame] | 4378 | |
| 4379 | if (monitor_is_qmp(mon)) { |
| 4380 | /* |
Markus Armbruster | c5f57ed | 2018-07-03 10:53:28 +0200 | [diff] [blame] | 4381 | * Kick I/O thread to make sure this takes effect. It'll be |
Peter Xu | e3e977d | 2018-03-09 16:59:56 +0800 | [diff] [blame] | 4382 | * evaluated again in prepare() of the watch object. |
| 4383 | */ |
Markus Armbruster | cab5ad8 | 2018-07-03 10:53:46 +0200 | [diff] [blame] | 4384 | aio_notify(iothread_get_aio_context(mon_iothread)); |
Peter Xu | e3e977d | 2018-03-09 16:59:56 +0800 | [diff] [blame] | 4385 | } |
| 4386 | |
| 4387 | trace_monitor_suspend(mon, 1); |
aliguori | cde76ee | 2009-03-05 23:01:51 +0000 | [diff] [blame] | 4388 | return 0; |
aliguori | d8f4460 | 2008-10-06 13:52:44 +0000 | [diff] [blame] | 4389 | } |
| 4390 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 4391 | void monitor_resume(Monitor *mon) |
aliguori | d8f4460 | 2008-10-06 13:52:44 +0000 | [diff] [blame] | 4392 | { |
Peter Xu | e3e977d | 2018-03-09 16:59:56 +0800 | [diff] [blame] | 4393 | if (monitor_is_hmp_non_interactive(mon)) { |
aliguori | cde76ee | 2009-03-05 23:01:51 +0000 | [diff] [blame] | 4394 | return; |
Peter Xu | df152fb | 2018-03-09 16:59:55 +0800 | [diff] [blame] | 4395 | } |
Peter Xu | e3e977d | 2018-03-09 16:59:56 +0800 | [diff] [blame] | 4396 | |
| 4397 | if (atomic_dec_fetch(&mon->suspend_cnt) == 0) { |
| 4398 | if (monitor_is_qmp(mon)) { |
| 4399 | /* |
Markus Armbruster | 774a6b6 | 2018-07-03 10:53:57 +0200 | [diff] [blame] | 4400 | * For QMP monitors that are running in the I/O thread, |
| 4401 | * let's kick the thread in case it's sleeping. |
Peter Xu | e3e977d | 2018-03-09 16:59:56 +0800 | [diff] [blame] | 4402 | */ |
Markus Armbruster | f91dc2a | 2018-07-03 10:53:45 +0200 | [diff] [blame] | 4403 | if (mon->use_io_thread) { |
Markus Armbruster | cab5ad8 | 2018-07-03 10:53:46 +0200 | [diff] [blame] | 4404 | aio_notify(iothread_get_aio_context(mon_iothread)); |
Peter Xu | e3e977d | 2018-03-09 16:59:56 +0800 | [diff] [blame] | 4405 | } |
| 4406 | } else { |
| 4407 | assert(mon->rs); |
| 4408 | readline_show_prompt(mon->rs); |
| 4409 | } |
| 4410 | } |
| 4411 | trace_monitor_suspend(mon, -1); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 4412 | } |
| 4413 | |
Markus Armbruster | 1816604 | 2018-07-03 10:53:51 +0200 | [diff] [blame] | 4414 | static QDict *qmp_greeting(Monitor *mon) |
Luiz Capitulino | ca9567e | 2010-02-04 18:10:04 -0200 | [diff] [blame] | 4415 | { |
Peter Xu | 0213031 | 2018-03-09 16:59:53 +0800 | [diff] [blame] | 4416 | QList *cap_list = qlist_new(); |
Luiz Capitulino | b9c15f1 | 2011-08-26 17:38:13 -0300 | [diff] [blame] | 4417 | QObject *ver = NULL; |
Peter Xu | 0213031 | 2018-03-09 16:59:53 +0800 | [diff] [blame] | 4418 | QMPCapability cap; |
Luiz Capitulino | ca9567e | 2010-02-04 18:10:04 -0200 | [diff] [blame] | 4419 | |
Markus Armbruster | 7fad30f | 2015-09-16 13:06:19 +0200 | [diff] [blame] | 4420 | qmp_marshal_query_version(NULL, &ver, NULL); |
Marc-André Lureau | c823501 | 2016-09-12 13:19:04 +0400 | [diff] [blame] | 4421 | |
Peter Xu | 0213031 | 2018-03-09 16:59:53 +0800 | [diff] [blame] | 4422 | for (cap = 0; cap < QMP_CAPABILITY__MAX; cap++) { |
Markus Armbruster | 279f9e0 | 2018-07-03 10:53:56 +0200 | [diff] [blame] | 4423 | if (mon->qmp.capab_offered[cap]) { |
| 4424 | qlist_append_str(cap_list, QMPCapability_str(cap)); |
Peter Xu | 0213031 | 2018-03-09 16:59:53 +0800 | [diff] [blame] | 4425 | } |
Peter Xu | 0213031 | 2018-03-09 16:59:53 +0800 | [diff] [blame] | 4426 | } |
| 4427 | |
Markus Armbruster | 1816604 | 2018-07-03 10:53:51 +0200 | [diff] [blame] | 4428 | return qdict_from_jsonf_nofail( |
| 4429 | "{'QMP': {'version': %p, 'capabilities': %p}}", |
| 4430 | ver, cap_list); |
Luiz Capitulino | ca9567e | 2010-02-04 18:10:04 -0200 | [diff] [blame] | 4431 | } |
| 4432 | |
Markus Armbruster | c83fe23 | 2015-03-06 19:20:51 +0100 | [diff] [blame] | 4433 | static void monitor_qmp_event(void *opaque, int event) |
Luiz Capitulino | 9b57c02 | 2009-11-26 22:58:58 -0200 | [diff] [blame] | 4434 | { |
Markus Armbruster | 1816604 | 2018-07-03 10:53:51 +0200 | [diff] [blame] | 4435 | QDict *data; |
Luiz Capitulino | 47116d1 | 2010-02-08 17:01:30 -0200 | [diff] [blame] | 4436 | Monitor *mon = opaque; |
Luiz Capitulino | 9b57c02 | 2009-11-26 22:58:58 -0200 | [diff] [blame] | 4437 | |
Luiz Capitulino | 47116d1 | 2010-02-08 17:01:30 -0200 | [diff] [blame] | 4438 | switch (event) { |
| 4439 | case CHR_EVENT_OPENED: |
Markus Armbruster | 635db18 | 2017-03-03 13:32:27 +0100 | [diff] [blame] | 4440 | mon->qmp.commands = &qmp_cap_negotiation_commands; |
Peter Xu | 0213031 | 2018-03-09 16:59:53 +0800 | [diff] [blame] | 4441 | monitor_qmp_caps_reset(mon); |
Markus Armbruster | 1816604 | 2018-07-03 10:53:51 +0200 | [diff] [blame] | 4442 | data = qmp_greeting(mon); |
| 4443 | qmp_queue_response(mon, data); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 4444 | qobject_unref(data); |
Corey Bryant | efb87c1 | 2012-08-14 16:43:48 -0400 | [diff] [blame] | 4445 | mon_refcount++; |
Luiz Capitulino | 47116d1 | 2010-02-08 17:01:30 -0200 | [diff] [blame] | 4446 | break; |
| 4447 | case CHR_EVENT_CLOSED: |
Peter Xu | c73a843 | 2018-06-20 15:32:19 +0800 | [diff] [blame] | 4448 | /* |
| 4449 | * Note: this is only useful when the output of the chardev |
| 4450 | * backend is still open. For example, when the backend is |
| 4451 | * stdio, it's possible that stdout is still open when stdin |
| 4452 | * is closed. |
| 4453 | */ |
| 4454 | monitor_qmp_response_flush(mon); |
Peter Xu | 6d2d563 | 2018-03-26 14:38:55 +0800 | [diff] [blame] | 4455 | monitor_qmp_cleanup_queues(mon); |
Markus Armbruster | 74358f2 | 2015-03-06 19:35:59 +0100 | [diff] [blame] | 4456 | json_message_parser_destroy(&mon->qmp.parser); |
| 4457 | json_message_parser_init(&mon->qmp.parser, handle_qmp_command); |
Corey Bryant | efb87c1 | 2012-08-14 16:43:48 -0400 | [diff] [blame] | 4458 | mon_refcount--; |
| 4459 | monitor_fdsets_cleanup(); |
Luiz Capitulino | 47116d1 | 2010-02-08 17:01:30 -0200 | [diff] [blame] | 4460 | break; |
Luiz Capitulino | 9b57c02 | 2009-11-26 22:58:58 -0200 | [diff] [blame] | 4461 | } |
| 4462 | } |
| 4463 | |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 4464 | static void monitor_event(void *opaque, int event) |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 4465 | { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 4466 | Monitor *mon = opaque; |
| 4467 | |
aliguori | 2724b18 | 2009-03-05 23:01:47 +0000 | [diff] [blame] | 4468 | switch (event) { |
| 4469 | case CHR_EVENT_MUX_IN: |
Peter Xu | dc7cbcd | 2018-06-08 11:55:05 +0800 | [diff] [blame] | 4470 | qemu_mutex_lock(&mon->mon_lock); |
Gerd Hoffmann | a7aec5d | 2009-09-10 10:58:54 +0200 | [diff] [blame] | 4471 | mon->mux_out = 0; |
Peter Xu | dc7cbcd | 2018-06-08 11:55:05 +0800 | [diff] [blame] | 4472 | qemu_mutex_unlock(&mon->mon_lock); |
Gerd Hoffmann | a7aec5d | 2009-09-10 10:58:54 +0200 | [diff] [blame] | 4473 | if (mon->reset_seen) { |
| 4474 | readline_restart(mon->rs); |
| 4475 | monitor_resume(mon); |
| 4476 | monitor_flush(mon); |
| 4477 | } else { |
Peter Xu | df152fb | 2018-03-09 16:59:55 +0800 | [diff] [blame] | 4478 | atomic_mb_set(&mon->suspend_cnt, 0); |
Gerd Hoffmann | a7aec5d | 2009-09-10 10:58:54 +0200 | [diff] [blame] | 4479 | } |
aliguori | 2724b18 | 2009-03-05 23:01:47 +0000 | [diff] [blame] | 4480 | break; |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 4481 | |
aliguori | 2724b18 | 2009-03-05 23:01:47 +0000 | [diff] [blame] | 4482 | case CHR_EVENT_MUX_OUT: |
Gerd Hoffmann | a7aec5d | 2009-09-10 10:58:54 +0200 | [diff] [blame] | 4483 | if (mon->reset_seen) { |
Peter Xu | df152fb | 2018-03-09 16:59:55 +0800 | [diff] [blame] | 4484 | if (atomic_mb_read(&mon->suspend_cnt) == 0) { |
Gerd Hoffmann | a7aec5d | 2009-09-10 10:58:54 +0200 | [diff] [blame] | 4485 | monitor_printf(mon, "\n"); |
| 4486 | } |
| 4487 | monitor_flush(mon); |
| 4488 | monitor_suspend(mon); |
| 4489 | } else { |
Peter Xu | df152fb | 2018-03-09 16:59:55 +0800 | [diff] [blame] | 4490 | atomic_inc(&mon->suspend_cnt); |
Gerd Hoffmann | a7aec5d | 2009-09-10 10:58:54 +0200 | [diff] [blame] | 4491 | } |
Peter Xu | dc7cbcd | 2018-06-08 11:55:05 +0800 | [diff] [blame] | 4492 | qemu_mutex_lock(&mon->mon_lock); |
Gerd Hoffmann | a7aec5d | 2009-09-10 10:58:54 +0200 | [diff] [blame] | 4493 | mon->mux_out = 1; |
Peter Xu | dc7cbcd | 2018-06-08 11:55:05 +0800 | [diff] [blame] | 4494 | qemu_mutex_unlock(&mon->mon_lock); |
aliguori | 2724b18 | 2009-03-05 23:01:47 +0000 | [diff] [blame] | 4495 | break; |
| 4496 | |
Amit Shah | b6b8df5 | 2009-10-07 18:31:16 +0530 | [diff] [blame] | 4497 | case CHR_EVENT_OPENED: |
aliguori | 2724b18 | 2009-03-05 23:01:47 +0000 | [diff] [blame] | 4498 | monitor_printf(mon, "QEMU %s monitor - type 'help' for more " |
| 4499 | "information\n", QEMU_VERSION); |
Gerd Hoffmann | a7aec5d | 2009-09-10 10:58:54 +0200 | [diff] [blame] | 4500 | if (!mon->mux_out) { |
Stratos Psomadakis | e5554e2 | 2014-09-15 15:34:57 +0300 | [diff] [blame] | 4501 | readline_restart(mon->rs); |
aliguori | 2724b18 | 2009-03-05 23:01:47 +0000 | [diff] [blame] | 4502 | readline_show_prompt(mon->rs); |
Gerd Hoffmann | a7aec5d | 2009-09-10 10:58:54 +0200 | [diff] [blame] | 4503 | } |
| 4504 | mon->reset_seen = 1; |
Corey Bryant | efb87c1 | 2012-08-14 16:43:48 -0400 | [diff] [blame] | 4505 | mon_refcount++; |
| 4506 | break; |
| 4507 | |
| 4508 | case CHR_EVENT_CLOSED: |
| 4509 | mon_refcount--; |
| 4510 | monitor_fdsets_cleanup(); |
aliguori | 2724b18 | 2009-03-05 23:01:47 +0000 | [diff] [blame] | 4511 | break; |
| 4512 | } |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 4513 | } |
| 4514 | |
Wayne Xia | 816f892 | 2011-10-12 11:32:41 +0800 | [diff] [blame] | 4515 | static int |
| 4516 | compare_mon_cmd(const void *a, const void *b) |
| 4517 | { |
| 4518 | return strcmp(((const mon_cmd_t *)a)->name, |
| 4519 | ((const mon_cmd_t *)b)->name); |
| 4520 | } |
| 4521 | |
| 4522 | static void sortcmdlist(void) |
| 4523 | { |
| 4524 | int array_num; |
| 4525 | int elem_size = sizeof(mon_cmd_t); |
| 4526 | |
| 4527 | array_num = sizeof(mon_cmds)/elem_size-1; |
| 4528 | qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd); |
| 4529 | |
| 4530 | array_num = sizeof(info_cmds)/elem_size-1; |
| 4531 | qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd); |
| 4532 | } |
| 4533 | |
Peter Xu | a5ed352 | 2018-03-09 16:59:52 +0800 | [diff] [blame] | 4534 | static GMainContext *monitor_get_io_context(void) |
| 4535 | { |
Markus Armbruster | cab5ad8 | 2018-07-03 10:53:46 +0200 | [diff] [blame] | 4536 | return iothread_get_g_main_context(mon_iothread); |
Peter Xu | a5ed352 | 2018-03-09 16:59:52 +0800 | [diff] [blame] | 4537 | } |
| 4538 | |
| 4539 | static AioContext *monitor_get_aio_context(void) |
| 4540 | { |
Markus Armbruster | cab5ad8 | 2018-07-03 10:53:46 +0200 | [diff] [blame] | 4541 | return iothread_get_aio_context(mon_iothread); |
Peter Xu | a5ed352 | 2018-03-09 16:59:52 +0800 | [diff] [blame] | 4542 | } |
| 4543 | |
| 4544 | static void monitor_iothread_init(void) |
| 4545 | { |
Markus Armbruster | cab5ad8 | 2018-07-03 10:53:46 +0200 | [diff] [blame] | 4546 | mon_iothread = iothread_create("mon_iothread", &error_abort); |
Peter Xu | 71da466 | 2018-03-09 16:59:57 +0800 | [diff] [blame] | 4547 | |
| 4548 | /* |
Markus Armbruster | 774a6b6 | 2018-07-03 10:53:57 +0200 | [diff] [blame] | 4549 | * The dispatcher BH must run in the main loop thread, since we |
| 4550 | * have commands assuming that context. It would be nice to get |
| 4551 | * rid of those assumptions. |
Peter Xu | 71da466 | 2018-03-09 16:59:57 +0800 | [diff] [blame] | 4552 | */ |
Markus Armbruster | cab5ad8 | 2018-07-03 10:53:46 +0200 | [diff] [blame] | 4553 | qmp_dispatcher_bh = aio_bh_new(iohandler_get_aio_context(), |
| 4554 | monitor_qmp_bh_dispatcher, |
| 4555 | NULL); |
Peter Xu | abe3cd0 | 2018-03-09 17:00:02 +0800 | [diff] [blame] | 4556 | |
| 4557 | /* |
Markus Armbruster | 774a6b6 | 2018-07-03 10:53:57 +0200 | [diff] [blame] | 4558 | * The responder BH must be run in the monitor I/O thread, so that |
| 4559 | * monitors that are using the I/O thread have their output |
| 4560 | * written by the I/O thread. |
Peter Xu | abe3cd0 | 2018-03-09 17:00:02 +0800 | [diff] [blame] | 4561 | */ |
Markus Armbruster | cab5ad8 | 2018-07-03 10:53:46 +0200 | [diff] [blame] | 4562 | qmp_respond_bh = aio_bh_new(monitor_get_aio_context(), |
| 4563 | monitor_qmp_bh_responder, |
| 4564 | NULL); |
Peter Xu | a5ed352 | 2018-03-09 16:59:52 +0800 | [diff] [blame] | 4565 | } |
| 4566 | |
Peter Xu | 6adf08d | 2018-03-09 16:59:50 +0800 | [diff] [blame] | 4567 | void monitor_init_globals(void) |
| 4568 | { |
| 4569 | monitor_init_qmp_commands(); |
| 4570 | monitor_qapi_event_init(); |
| 4571 | sortcmdlist(); |
| 4572 | qemu_mutex_init(&monitor_lock); |
Peter Xu | 4745146 | 2018-06-08 11:55:11 +0800 | [diff] [blame] | 4573 | qemu_mutex_init(&mon_fdsets_lock); |
Peter Xu | a5ed352 | 2018-03-09 16:59:52 +0800 | [diff] [blame] | 4574 | monitor_iothread_init(); |
Peter Xu | 6adf08d | 2018-03-09 16:59:50 +0800 | [diff] [blame] | 4575 | } |
| 4576 | |
Stefan Hajnoczi | c60bf33 | 2013-11-14 11:54:14 +0100 | [diff] [blame] | 4577 | /* These functions just adapt the readline interface in a typesafe way. We |
| 4578 | * could cast function pointers but that discards compiler checks. |
| 4579 | */ |
Stefan Weil | d5d1507 | 2014-01-25 18:18:23 +0100 | [diff] [blame] | 4580 | static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque, |
| 4581 | const char *fmt, ...) |
Stefan Hajnoczi | c60bf33 | 2013-11-14 11:54:14 +0100 | [diff] [blame] | 4582 | { |
| 4583 | va_list ap; |
| 4584 | va_start(ap, fmt); |
| 4585 | monitor_vprintf(opaque, fmt, ap); |
| 4586 | va_end(ap); |
| 4587 | } |
| 4588 | |
| 4589 | static void monitor_readline_flush(void *opaque) |
| 4590 | { |
| 4591 | monitor_flush(opaque); |
| 4592 | } |
| 4593 | |
Paolo Bonzini | 397d30e | 2016-10-24 18:31:02 +0200 | [diff] [blame] | 4594 | /* |
| 4595 | * Print to current monitor if we have one, else to stderr. |
| 4596 | * TODO should return int, so callers can calculate width, but that |
| 4597 | * requires surgery to monitor_vprintf(). Left for another day. |
| 4598 | */ |
| 4599 | void error_vprintf(const char *fmt, va_list ap) |
| 4600 | { |
| 4601 | if (cur_mon && !monitor_cur_is_qmp()) { |
| 4602 | monitor_vprintf(cur_mon, fmt, ap); |
| 4603 | } else { |
| 4604 | vfprintf(stderr, fmt, ap); |
| 4605 | } |
| 4606 | } |
| 4607 | |
| 4608 | void error_vprintf_unless_qmp(const char *fmt, va_list ap) |
| 4609 | { |
| 4610 | if (cur_mon && !monitor_cur_is_qmp()) { |
| 4611 | monitor_vprintf(cur_mon, fmt, ap); |
Marc-André Lureau | 0d6b50d | 2017-01-05 14:59:57 +0100 | [diff] [blame] | 4612 | } else if (!cur_mon) { |
| 4613 | vfprintf(stderr, fmt, ap); |
Paolo Bonzini | 397d30e | 2016-10-24 18:31:02 +0200 | [diff] [blame] | 4614 | } |
| 4615 | } |
| 4616 | |
Peter Xu | a5ed352 | 2018-03-09 16:59:52 +0800 | [diff] [blame] | 4617 | static void monitor_list_append(Monitor *mon) |
| 4618 | { |
| 4619 | qemu_mutex_lock(&monitor_lock); |
| 4620 | QTAILQ_INSERT_HEAD(&mon_list, mon, entry); |
| 4621 | qemu_mutex_unlock(&monitor_lock); |
| 4622 | } |
| 4623 | |
| 4624 | static void monitor_qmp_setup_handlers_bh(void *opaque) |
| 4625 | { |
| 4626 | Monitor *mon = opaque; |
| 4627 | GMainContext *context; |
| 4628 | |
Markus Armbruster | f91dc2a | 2018-07-03 10:53:45 +0200 | [diff] [blame] | 4629 | if (mon->use_io_thread) { |
Markus Armbruster | 774a6b6 | 2018-07-03 10:53:57 +0200 | [diff] [blame] | 4630 | /* Use @mon_iothread context */ |
Peter Xu | a5ed352 | 2018-03-09 16:59:52 +0800 | [diff] [blame] | 4631 | context = monitor_get_io_context(); |
Peter Xu | a5ed352 | 2018-03-09 16:59:52 +0800 | [diff] [blame] | 4632 | assert(context); |
| 4633 | } else { |
Markus Armbruster | 774a6b6 | 2018-07-03 10:53:57 +0200 | [diff] [blame] | 4634 | /* Use default main loop context */ |
Peter Xu | a5ed352 | 2018-03-09 16:59:52 +0800 | [diff] [blame] | 4635 | context = NULL; |
| 4636 | } |
| 4637 | |
| 4638 | qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, monitor_qmp_read, |
| 4639 | monitor_qmp_event, NULL, mon, context, true); |
| 4640 | monitor_list_append(mon); |
| 4641 | } |
| 4642 | |
Marc-André Lureau | 0ec7b3e | 2016-12-07 16:20:22 +0300 | [diff] [blame] | 4643 | void monitor_init(Chardev *chr, int flags) |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 4644 | { |
Peter Xu | 6adf08d | 2018-03-09 16:59:50 +0800 | [diff] [blame] | 4645 | Monitor *mon = g_malloc(sizeof(*mon)); |
Peter Xu | be933ff | 2018-03-26 14:38:56 +0800 | [diff] [blame] | 4646 | bool use_readline = flags & MONITOR_USE_READLINE; |
| 4647 | bool use_oob = flags & MONITOR_USE_OOB; |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 4648 | |
Peter Xu | be933ff | 2018-03-26 14:38:56 +0800 | [diff] [blame] | 4649 | if (use_oob) { |
| 4650 | if (CHARDEV_IS_MUX(chr)) { |
Markus Armbruster | c069821 | 2018-07-03 10:53:27 +0200 | [diff] [blame] | 4651 | error_report("Monitor out-of-band is not supported with " |
Peter Xu | be933ff | 2018-03-26 14:38:56 +0800 | [diff] [blame] | 4652 | "MUX typed chardev backend"); |
| 4653 | exit(1); |
| 4654 | } |
| 4655 | if (use_readline) { |
Markus Armbruster | c069821 | 2018-07-03 10:53:27 +0200 | [diff] [blame] | 4656 | error_report("Monitor out-of-band is only supported by QMP"); |
Peter Xu | be933ff | 2018-03-26 14:38:56 +0800 | [diff] [blame] | 4657 | exit(1); |
| 4658 | } |
| 4659 | } |
| 4660 | |
| 4661 | monitor_data_init(mon, false, use_oob); |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 4662 | |
Marc-André Lureau | 32a6ebe | 2016-10-22 12:52:52 +0300 | [diff] [blame] | 4663 | qemu_chr_fe_init(&mon->chr, chr, &error_abort); |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 4664 | mon->flags = flags; |
Peter Xu | be933ff | 2018-03-26 14:38:56 +0800 | [diff] [blame] | 4665 | if (use_readline) { |
Stefan Hajnoczi | c60bf33 | 2013-11-14 11:54:14 +0100 | [diff] [blame] | 4666 | mon->rs = readline_init(monitor_readline_printf, |
| 4667 | monitor_readline_flush, |
| 4668 | mon, |
| 4669 | monitor_find_completion); |
aliguori | cde76ee | 2009-03-05 23:01:51 +0000 | [diff] [blame] | 4670 | monitor_read_command(mon, 0); |
| 4671 | } |
aliguori | 8712716 | 2009-03-05 23:01:29 +0000 | [diff] [blame] | 4672 | |
Markus Armbruster | 9f3982f | 2015-03-06 19:56:38 +0100 | [diff] [blame] | 4673 | if (monitor_is_qmp(mon)) { |
Marc-André Lureau | 5345fdb | 2016-10-22 12:52:55 +0300 | [diff] [blame] | 4674 | qemu_chr_fe_set_echo(&mon->chr, true); |
Markus Armbruster | 74358f2 | 2015-03-06 19:35:59 +0100 | [diff] [blame] | 4675 | json_message_parser_init(&mon->qmp.parser, handle_qmp_command); |
Markus Armbruster | f91dc2a | 2018-07-03 10:53:45 +0200 | [diff] [blame] | 4676 | if (mon->use_io_thread) { |
Peter Xu | a5ed352 | 2018-03-09 16:59:52 +0800 | [diff] [blame] | 4677 | /* |
| 4678 | * Make sure the old iowatch is gone. It's possible when |
| 4679 | * e.g. the chardev is in client mode, with wait=on. |
| 4680 | */ |
| 4681 | remove_fd_in_watch(chr); |
| 4682 | /* |
| 4683 | * We can't call qemu_chr_fe_set_handlers() directly here |
Markus Armbruster | 774a6b6 | 2018-07-03 10:53:57 +0200 | [diff] [blame] | 4684 | * since chardev might be running in the monitor I/O |
| 4685 | * thread. Schedule a bottom half. |
Peter Xu | a5ed352 | 2018-03-09 16:59:52 +0800 | [diff] [blame] | 4686 | */ |
| 4687 | aio_bh_schedule_oneshot(monitor_get_aio_context(), |
| 4688 | monitor_qmp_setup_handlers_bh, mon); |
Markus Armbruster | 774a6b6 | 2018-07-03 10:53:57 +0200 | [diff] [blame] | 4689 | /* The bottom half will add @mon to @mon_list */ |
Peter Xu | a5ed352 | 2018-03-09 16:59:52 +0800 | [diff] [blame] | 4690 | return; |
| 4691 | } else { |
| 4692 | qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, |
| 4693 | monitor_qmp_read, monitor_qmp_event, |
| 4694 | NULL, mon, NULL, true); |
| 4695 | } |
Luiz Capitulino | 9b57c02 | 2009-11-26 22:58:58 -0200 | [diff] [blame] | 4696 | } else { |
Marc-André Lureau | 5345fdb | 2016-10-22 12:52:55 +0300 | [diff] [blame] | 4697 | qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, monitor_read, |
Anton Nefedov | 81517ba | 2017-07-06 15:08:49 +0300 | [diff] [blame] | 4698 | monitor_event, NULL, mon, NULL, true); |
Luiz Capitulino | 9b57c02 | 2009-11-26 22:58:58 -0200 | [diff] [blame] | 4699 | } |
aliguori | 8712716 | 2009-03-05 23:01:29 +0000 | [diff] [blame] | 4700 | |
Peter Xu | a5ed352 | 2018-03-09 16:59:52 +0800 | [diff] [blame] | 4701 | monitor_list_append(mon); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 4702 | } |
| 4703 | |
Marc-André Lureau | 2ef4571 | 2016-08-01 15:23:42 +0400 | [diff] [blame] | 4704 | void monitor_cleanup(void) |
| 4705 | { |
| 4706 | Monitor *mon, *next; |
| 4707 | |
Peter Xu | a5ed352 | 2018-03-09 16:59:52 +0800 | [diff] [blame] | 4708 | /* |
Markus Armbruster | c5f57ed | 2018-07-03 10:53:28 +0200 | [diff] [blame] | 4709 | * We need to explicitly stop the I/O thread (but not destroy it), |
Markus Armbruster | 774a6b6 | 2018-07-03 10:53:57 +0200 | [diff] [blame] | 4710 | * clean up the monitor resources, then destroy the I/O thread since |
Peter Xu | a5ed352 | 2018-03-09 16:59:52 +0800 | [diff] [blame] | 4711 | * we need to unregister from chardev below in |
| 4712 | * monitor_data_destroy(), and chardev is not thread-safe yet |
| 4713 | */ |
Markus Armbruster | cab5ad8 | 2018-07-03 10:53:46 +0200 | [diff] [blame] | 4714 | iothread_stop(mon_iothread); |
Peter Xu | a5ed352 | 2018-03-09 16:59:52 +0800 | [diff] [blame] | 4715 | |
Peter Xu | abe3cd0 | 2018-03-09 17:00:02 +0800 | [diff] [blame] | 4716 | /* |
Markus Armbruster | 774a6b6 | 2018-07-03 10:53:57 +0200 | [diff] [blame] | 4717 | * Flush all response queues. Note that even after this flush, |
| 4718 | * data may remain in output buffers. |
Peter Xu | abe3cd0 | 2018-03-09 17:00:02 +0800 | [diff] [blame] | 4719 | */ |
| 4720 | monitor_qmp_bh_responder(NULL); |
| 4721 | |
Markus Armbruster | 774a6b6 | 2018-07-03 10:53:57 +0200 | [diff] [blame] | 4722 | /* Flush output buffers and destroy monitors */ |
Marc-André Lureau | 2ef4571 | 2016-08-01 15:23:42 +0400 | [diff] [blame] | 4723 | qemu_mutex_lock(&monitor_lock); |
Peter Xu | 238d9f3 | 2018-03-09 16:59:51 +0800 | [diff] [blame] | 4724 | QTAILQ_FOREACH_SAFE(mon, &mon_list, entry, next) { |
| 4725 | QTAILQ_REMOVE(&mon_list, mon, entry); |
Peter Xu | abe3cd0 | 2018-03-09 17:00:02 +0800 | [diff] [blame] | 4726 | monitor_flush(mon); |
Marc-André Lureau | 2ef4571 | 2016-08-01 15:23:42 +0400 | [diff] [blame] | 4727 | monitor_data_destroy(mon); |
| 4728 | g_free(mon); |
| 4729 | } |
| 4730 | qemu_mutex_unlock(&monitor_lock); |
Peter Xu | a5ed352 | 2018-03-09 16:59:52 +0800 | [diff] [blame] | 4731 | |
Markus Armbruster | c5f57ed | 2018-07-03 10:53:28 +0200 | [diff] [blame] | 4732 | /* QEMUBHs needs to be deleted before destroying the I/O thread */ |
Markus Armbruster | cab5ad8 | 2018-07-03 10:53:46 +0200 | [diff] [blame] | 4733 | qemu_bh_delete(qmp_dispatcher_bh); |
| 4734 | qmp_dispatcher_bh = NULL; |
| 4735 | qemu_bh_delete(qmp_respond_bh); |
| 4736 | qmp_respond_bh = NULL; |
Peter Xu | 71da466 | 2018-03-09 16:59:57 +0800 | [diff] [blame] | 4737 | |
Markus Armbruster | cab5ad8 | 2018-07-03 10:53:46 +0200 | [diff] [blame] | 4738 | iothread_destroy(mon_iothread); |
| 4739 | mon_iothread = NULL; |
Marc-André Lureau | 2ef4571 | 2016-08-01 15:23:42 +0400 | [diff] [blame] | 4740 | } |
| 4741 | |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 4742 | QemuOptsList qemu_mon_opts = { |
| 4743 | .name = "mon", |
| 4744 | .implied_opt_name = "chardev", |
| 4745 | .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head), |
| 4746 | .desc = { |
| 4747 | { |
| 4748 | .name = "mode", |
| 4749 | .type = QEMU_OPT_STRING, |
| 4750 | },{ |
| 4751 | .name = "chardev", |
| 4752 | .type = QEMU_OPT_STRING, |
| 4753 | },{ |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 4754 | .name = "pretty", |
| 4755 | .type = QEMU_OPT_BOOL, |
Peter Xu | be933ff | 2018-03-26 14:38:56 +0800 | [diff] [blame] | 4756 | },{ |
| 4757 | .name = "x-oob", |
| 4758 | .type = QEMU_OPT_BOOL, |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 4759 | }, |
| 4760 | { /* end of list */ } |
| 4761 | }, |
| 4762 | }; |
Marcelo Tosatti | f2ae8ab | 2014-06-24 18:55:11 -0300 | [diff] [blame] | 4763 | |
| 4764 | #ifndef TARGET_I386 |
| 4765 | void qmp_rtc_reset_reinjection(Error **errp) |
| 4766 | { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 4767 | error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection"); |
Marcelo Tosatti | f2ae8ab | 2014-06-24 18:55:11 -0300 | [diff] [blame] | 4768 | } |
Brijesh Singh | 08a161f | 2018-03-08 06:48:42 -0600 | [diff] [blame] | 4769 | |
| 4770 | SevInfo *qmp_query_sev(Error **errp) |
| 4771 | { |
| 4772 | error_setg(errp, QERR_FEATURE_DISABLED, "query-sev"); |
| 4773 | return NULL; |
| 4774 | } |
Brijesh Singh | 1b6a034 | 2018-03-08 06:48:56 -0600 | [diff] [blame] | 4775 | |
| 4776 | SevLaunchMeasureInfo *qmp_query_sev_launch_measure(Error **errp) |
| 4777 | { |
| 4778 | error_setg(errp, QERR_FEATURE_DISABLED, "query-sev-launch-measure"); |
| 4779 | return NULL; |
| 4780 | } |
Brijesh Singh | 31dd67f | 2018-03-08 06:48:59 -0600 | [diff] [blame] | 4781 | |
| 4782 | SevCapability *qmp_query_sev_capabilities(Error **errp) |
| 4783 | { |
| 4784 | error_setg(errp, QERR_FEATURE_DISABLED, "query-sev-capabilities"); |
| 4785 | return NULL; |
| 4786 | } |
Marcelo Tosatti | f2ae8ab | 2014-06-24 18:55:11 -0300 | [diff] [blame] | 4787 | #endif |
Jason J. Herne | 7ee0c3e | 2015-06-26 14:03:16 -0400 | [diff] [blame] | 4788 | |
| 4789 | #ifndef TARGET_S390X |
| 4790 | void qmp_dump_skeys(const char *filename, Error **errp) |
| 4791 | { |
| 4792 | error_setg(errp, QERR_FEATURE_DISABLED, "dump-skeys"); |
| 4793 | } |
| 4794 | #endif |
Peter Xu | ae50a77 | 2016-03-30 17:27:24 +0100 | [diff] [blame] | 4795 | |
| 4796 | #ifndef TARGET_ARM |
| 4797 | GICCapabilityList *qmp_query_gic_capabilities(Error **errp) |
| 4798 | { |
| 4799 | error_setg(errp, QERR_FEATURE_DISABLED, "query-gic-capabilities"); |
| 4800 | return NULL; |
| 4801 | } |
| 4802 | #endif |
Igor Mammedov | d463354 | 2016-06-10 06:29:06 +0530 | [diff] [blame] | 4803 | |
| 4804 | HotpluggableCPUList *qmp_query_hotpluggable_cpus(Error **errp) |
| 4805 | { |
| 4806 | MachineState *ms = MACHINE(qdev_get_machine()); |
| 4807 | MachineClass *mc = MACHINE_GET_CLASS(ms); |
| 4808 | |
Igor Mammedov | c5514d0 | 2017-02-10 11:20:57 +0100 | [diff] [blame] | 4809 | if (!mc->has_hotpluggable_cpus) { |
Igor Mammedov | d463354 | 2016-06-10 06:29:06 +0530 | [diff] [blame] | 4810 | error_setg(errp, QERR_FEATURE_DISABLED, "query-hotpluggable-cpus"); |
| 4811 | return NULL; |
| 4812 | } |
| 4813 | |
Igor Mammedov | c5514d0 | 2017-02-10 11:20:57 +0100 | [diff] [blame] | 4814 | return machine_query_hotpluggable_cpus(ms); |
Igor Mammedov | d463354 | 2016-06-10 06:29:06 +0530 | [diff] [blame] | 4815 | } |