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 | */ |
Peter Maydell | d38ea87 | 2016-01-29 17:50:05 +0000 | [diff] [blame] | 24 | #include "qemu/osdep.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 25 | #include <dirent.h> |
Paolo Bonzini | 33c1187 | 2016-03-15 16:58:45 +0100 | [diff] [blame] | 26 | #include "qemu-common.h" |
| 27 | #include "cpu.h" |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 28 | #include "hw/hw.h" |
Paolo Bonzini | b4a42f8 | 2013-02-04 11:37:52 +0100 | [diff] [blame] | 29 | #include "monitor/qdev.h" |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 30 | #include "hw/usb.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 31 | #include "hw/i386/pc.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" |
Paolo Bonzini | dccfcd0 | 2013-04-08 16:55:25 +0200 | [diff] [blame] | 38 | #include "sysemu/char.h" |
Gerd Hoffmann | 7572150 | 2010-10-07 12:22:54 +0200 | [diff] [blame] | 39 | #include "ui/qemu-spice.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 40 | #include "sysemu/sysemu.h" |
Eduardo Habkost | e35704b | 2015-02-08 16:51:16 -0200 | [diff] [blame] | 41 | #include "sysemu/numa.h" |
Paolo Bonzini | 83c9089 | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 42 | #include "monitor/monitor.h" |
Stefan Hajnoczi | 0150cd8 | 2013-11-14 11:54:15 +0100 | [diff] [blame] | 43 | #include "qemu/readline.h" |
Paolo Bonzini | 28ecbae | 2012-11-28 12:06:30 +0100 | [diff] [blame] | 44 | #include "ui/console.h" |
Gerd Hoffmann | c751a74 | 2013-12-04 15:02:28 +0100 | [diff] [blame] | 45 | #include "ui/input.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 46 | #include "sysemu/blockdev.h" |
Max Reitz | da27a00 | 2016-03-16 19:54:29 +0100 | [diff] [blame] | 47 | #include "sysemu/block-backend.h" |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 48 | #include "audio/audio.h" |
Paolo Bonzini | 76cad71 | 2012-10-24 11:12:21 +0200 | [diff] [blame] | 49 | #include "disas/disas.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 50 | #include "sysemu/balloon.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 51 | #include "qemu/timer.h" |
Paolo Bonzini | caf71f8 | 2012-12-17 18:19:50 +0100 | [diff] [blame] | 52 | #include "migration/migration.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 53 | #include "sysemu/kvm.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 | cc7a8ea | 2015-03-17 17:22:46 +0100 | [diff] [blame] | 56 | #include "qapi/qmp/qerror.h" |
Eric Blake | c7eb39c | 2016-06-09 10:48:32 -0600 | [diff] [blame] | 57 | #include "qapi/qmp/types.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 58 | #include "qapi/qmp/qjson.h" |
| 59 | #include "qapi/qmp/json-streamer.h" |
| 60 | #include "qapi/qmp/json-parser.h" |
Markus Armbruster | a9c9427 | 2016-06-22 19:11:19 +0200 | [diff] [blame] | 61 | #include "qom/object_interfaces.h" |
Blue Swirl | 2b41f10 | 2011-06-19 20:38:22 +0000 | [diff] [blame] | 62 | #include "cpu.h" |
Stefan Hajnoczi | 89bd820 | 2011-09-23 08:23:06 +0100 | [diff] [blame] | 63 | #include "trace.h" |
Lluís | 31965ae | 2011-08-31 20:31:24 +0200 | [diff] [blame] | 64 | #include "trace/control.h" |
Pavel Butsykin | bf95728 | 2015-09-10 18:38:59 +0300 | [diff] [blame] | 65 | #include "monitor/hmp-target.h" |
Lluís | 6d8a764 | 2011-08-31 20:30:43 +0200 | [diff] [blame] | 66 | #ifdef CONFIG_TRACE_SIMPLE |
Lluís | 31965ae | 2011-08-31 20:31:24 +0200 | [diff] [blame] | 67 | #include "trace/simple.h" |
Prerna Saxena | 22890ab | 2010-06-24 17:04:53 +0530 | [diff] [blame] | 68 | #endif |
Paolo Bonzini | 022c62c | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 69 | #include "exec/memory.h" |
Paolo Bonzini | 63c9155 | 2016-03-15 13:18:37 +0100 | [diff] [blame] | 70 | #include "exec/exec-all.h" |
Paolo Bonzini | 03dd024 | 2015-12-15 13:16:16 +0100 | [diff] [blame] | 71 | #include "qemu/log.h" |
Anthony Liguori | 48a32be | 2011-09-02 12:34:48 -0500 | [diff] [blame] | 72 | #include "qmp-commands.h" |
| 73 | #include "hmp.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 74 | #include "qemu/thread.h" |
Hani Benhabiles | b21631f | 2014-05-27 23:39:37 +0100 | [diff] [blame] | 75 | #include "block/qapi.h" |
Wenchao Xia | 43a14cf | 2014-06-18 08:43:31 +0200 | [diff] [blame] | 76 | #include "qapi/qmp-event.h" |
| 77 | #include "qapi-event.h" |
Markus Armbruster | 39a1815 | 2015-09-16 13:06:28 +0200 | [diff] [blame] | 78 | #include "qmp-introspect.h" |
Fam Zheng | 5560625 | 2015-03-02 19:36:46 +0800 | [diff] [blame] | 79 | #include "sysemu/block-backend.h" |
Alberto Garcia | dc59997 | 2016-03-10 13:55:26 +0200 | [diff] [blame] | 80 | #include "sysemu/qtest.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 81 | #include "qemu/cutils.h" |
Marc-André Lureau | edcfaef | 2016-09-12 13:19:00 +0400 | [diff] [blame] | 82 | #include "qapi/qmp/dispatch.h" |
ths | 6a5bd30 | 2007-12-03 17:05:38 +0000 | [diff] [blame] | 83 | |
Jason J. Herne | a4538a5 | 2015-06-26 14:07:21 -0400 | [diff] [blame] | 84 | #if defined(TARGET_S390X) |
| 85 | #include "hw/s390x/storage-keys.h" |
| 86 | #endif |
| 87 | |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 88 | /* |
| 89 | * Supported types: |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 90 | * |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 91 | * 'F' filename |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 92 | * 'B' block device name |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 93 | * 's' string (accept optional quote) |
Wenchao Xia | 129be00 | 2013-08-27 20:38:26 +0800 | [diff] [blame] | 94 | * 'S' it just appends the rest of the string (accept optional quote) |
Markus Armbruster | 361127d | 2010-02-10 20:24:35 +0100 | [diff] [blame] | 95 | * 'O' option string of the form NAME=VALUE,... |
| 96 | * parsed according to QemuOptsList given by its name |
| 97 | * Example: 'device:O' uses qemu_device_opts. |
| 98 | * Restriction: only lists with empty desc are supported |
| 99 | * TODO lift the restriction |
bellard | 92a31b1 | 2005-02-10 22:00:52 +0000 | [diff] [blame] | 100 | * 'i' 32 bit integer |
| 101 | * 'l' target long (32 or 64 bit) |
Luiz Capitulino | 9116284 | 2012-04-26 17:34:30 -0300 | [diff] [blame] | 102 | * 'M' Non-negative target long (32 or 64 bit), in user mode the |
| 103 | * value is multiplied by 2^20 (think Mebibyte) |
Jes Sorensen | dbc0c67 | 2010-10-21 17:15:47 +0200 | [diff] [blame] | 104 | * 'o' octets (aka bytes) |
Kevin Wolf | 5e00984 | 2013-06-05 14:19:27 +0200 | [diff] [blame] | 105 | * user mode accepts an optional E, e, P, p, T, t, G, g, M, m, |
| 106 | * K, k suffix, which multiplies the value by 2^60 for suffixes E |
| 107 | * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t, |
| 108 | * 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] | 109 | * 'T' double |
| 110 | * user mode accepts an optional ms, us, ns suffix, |
| 111 | * which divides the value by 1e3, 1e6, 1e9, respectively |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 112 | * '/' optional gdb-like print format (like "/10x") |
| 113 | * |
Luiz Capitulino | fb46660 | 2009-08-28 15:27:27 -0300 | [diff] [blame] | 114 | * '?' optional type (for all types, except '/') |
| 115 | * '.' other form of optional type (for 'i' and 'l') |
Markus Armbruster | 942cd1f | 2010-03-26 09:07:09 +0100 | [diff] [blame] | 116 | * 'b' boolean |
| 117 | * user mode accepts "on" or "off" |
Luiz Capitulino | fb46660 | 2009-08-28 15:27:27 -0300 | [diff] [blame] | 118 | * '-' optional parameter (eg. '-f') |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 119 | * |
| 120 | */ |
| 121 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 122 | typedef struct mon_cmd_t { |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 123 | const char *name; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 124 | const char *args_type; |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 125 | const char *params; |
| 126 | const char *help; |
Marc-André Lureau | 2b9e357 | 2016-09-12 13:19:06 +0400 | [diff] [blame] | 127 | void (*cmd)(Monitor *mon, const QDict *qdict); |
| 128 | /* @sub_table is a list of 2nd level of commands. If it does not exist, |
| 129 | * cmd should be used. If it exists, sub_table[?].cmd should be |
| 130 | * used, and cmd of 1st level plays the role of help function. |
Wenchao Xia | 5f3d335 | 2013-01-14 14:06:27 +0800 | [diff] [blame] | 131 | */ |
| 132 | struct mon_cmd_t *sub_table; |
Hani Benhabiles | bfa40f7 | 2014-04-13 16:25:06 +0100 | [diff] [blame] | 133 | void (*command_completion)(ReadLineState *rs, int nb_args, const char *str); |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 134 | } mon_cmd_t; |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 135 | |
Mark McLoughlin | f07918f | 2009-07-22 09:11:40 +0100 | [diff] [blame] | 136 | /* file descriptors passed via SCM_RIGHTS */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 137 | typedef struct mon_fd_t mon_fd_t; |
| 138 | struct mon_fd_t { |
Mark McLoughlin | f07918f | 2009-07-22 09:11:40 +0100 | [diff] [blame] | 139 | char *name; |
| 140 | int fd; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 141 | QLIST_ENTRY(mon_fd_t) next; |
Mark McLoughlin | f07918f | 2009-07-22 09:11:40 +0100 | [diff] [blame] | 142 | }; |
| 143 | |
Corey Bryant | ba1c048 | 2012-08-14 16:43:43 -0400 | [diff] [blame] | 144 | /* file descriptor associated with a file descriptor set */ |
| 145 | typedef struct MonFdsetFd MonFdsetFd; |
| 146 | struct MonFdsetFd { |
| 147 | int fd; |
| 148 | bool removed; |
| 149 | char *opaque; |
| 150 | QLIST_ENTRY(MonFdsetFd) next; |
| 151 | }; |
| 152 | |
| 153 | /* file descriptor set containing fds passed via SCM_RIGHTS */ |
| 154 | typedef struct MonFdset MonFdset; |
| 155 | struct MonFdset { |
| 156 | int64_t id; |
| 157 | QLIST_HEAD(, MonFdsetFd) fds; |
Corey Bryant | adb696f | 2012-08-14 16:43:47 -0400 | [diff] [blame] | 158 | QLIST_HEAD(, MonFdsetFd) dup_fds; |
Corey Bryant | ba1c048 | 2012-08-14 16:43:43 -0400 | [diff] [blame] | 159 | QLIST_ENTRY(MonFdset) next; |
| 160 | }; |
| 161 | |
Markus Armbruster | 74358f2 | 2015-03-06 19:35:59 +0100 | [diff] [blame] | 162 | typedef struct { |
Luiz Capitulino | 5fa737a | 2009-11-26 22:59:01 -0200 | [diff] [blame] | 163 | JSONMessageParser parser; |
Markus Armbruster | f994b25 | 2015-03-06 19:51:51 +0100 | [diff] [blame] | 164 | /* |
| 165 | * When a client connects, we're in capabilities negotiation mode. |
| 166 | * When command qmp_capabilities succeeds, we go into command |
| 167 | * mode. |
| 168 | */ |
| 169 | bool in_command_mode; /* are we in command mode? */ |
Markus Armbruster | 74358f2 | 2015-03-06 19:35:59 +0100 | [diff] [blame] | 170 | } MonitorQMP; |
Luiz Capitulino | 5fa737a | 2009-11-26 22:59:01 -0200 | [diff] [blame] | 171 | |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 172 | /* |
| 173 | * To prevent flooding clients, events can be throttled. The |
| 174 | * throttling is calculated globally, rather than per-Monitor |
| 175 | * instance. |
| 176 | */ |
Wenchao Xia | 43a14cf | 2014-06-18 08:43:31 +0200 | [diff] [blame] | 177 | typedef struct MonitorQAPIEventState { |
Markus Armbruster | 7de0be6 | 2015-10-15 17:08:35 +0200 | [diff] [blame] | 178 | QAPIEvent event; /* Throttling state for this event type and... */ |
| 179 | QDict *data; /* ... data, see qapi_event_throttle_equal() */ |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 180 | QEMUTimer *timer; /* Timer for handling delayed events */ |
Markus Armbruster | 688b4b7 | 2015-10-15 17:08:30 +0200 | [diff] [blame] | 181 | QDict *qdict; /* Delayed event (if any) */ |
Wenchao Xia | 43a14cf | 2014-06-18 08:43:31 +0200 | [diff] [blame] | 182 | } MonitorQAPIEventState; |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 183 | |
Markus Armbruster | b9b03ab | 2015-10-15 17:08:33 +0200 | [diff] [blame] | 184 | typedef struct { |
| 185 | int64_t rate; /* Minimum time (in ns) between two events */ |
| 186 | } MonitorQAPIEventConf; |
| 187 | |
aliguori | 8712716 | 2009-03-05 23:01:29 +0000 | [diff] [blame] | 188 | struct Monitor { |
| 189 | CharDriverState *chr; |
Gerd Hoffmann | a7aec5d | 2009-09-10 10:58:54 +0200 | [diff] [blame] | 190 | int reset_seen; |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 191 | int flags; |
| 192 | int suspend_cnt; |
Luiz Capitulino | 48c043d | 2013-04-02 15:07:33 -0400 | [diff] [blame] | 193 | bool skip_flush; |
Paolo Bonzini | 6cff3e8 | 2014-06-18 08:43:59 +0200 | [diff] [blame] | 194 | |
| 195 | QemuMutex out_lock; |
Luiz Capitulino | e1f2641 | 2013-03-25 13:52:26 -0400 | [diff] [blame] | 196 | QString *outbuf; |
Paolo Bonzini | 6cff3e8 | 2014-06-18 08:43:59 +0200 | [diff] [blame] | 197 | guint out_watch; |
| 198 | |
| 199 | /* Read under either BQL or out_lock, written with BQL+out_lock. */ |
| 200 | int mux_out; |
| 201 | |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 202 | ReadLineState *rs; |
Markus Armbruster | 74358f2 | 2015-03-06 19:35:59 +0100 | [diff] [blame] | 203 | MonitorQMP qmp; |
Andreas Färber | cb446ec | 2013-05-01 14:24:52 +0200 | [diff] [blame] | 204 | CPUState *mon_cpu; |
Markus Armbruster | 097310b | 2014-10-07 13:59:15 +0200 | [diff] [blame] | 205 | BlockCompletionFunc *password_completion_cb; |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 206 | void *password_opaque; |
Wenchao Xia | 7717239 | 2013-08-27 20:38:20 +0800 | [diff] [blame] | 207 | mon_cmd_t *cmd_table; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 208 | QLIST_HEAD(,mon_fd_t) fds; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 209 | QLIST_ENTRY(Monitor) entry; |
aliguori | 8712716 | 2009-03-05 23:01:29 +0000 | [diff] [blame] | 210 | }; |
| 211 | |
Luiz Capitulino | 2dbc8db | 2010-05-26 16:13:09 -0300 | [diff] [blame] | 212 | /* QMP checker flags */ |
| 213 | #define QMP_ACCEPT_UNKNOWNS 1 |
| 214 | |
Paolo Bonzini | d622cb5 | 2014-06-18 08:44:00 +0200 | [diff] [blame] | 215 | /* Protects mon_list, monitor_event_state. */ |
| 216 | static QemuMutex monitor_lock; |
| 217 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 218 | static QLIST_HEAD(mon_list, Monitor) mon_list; |
Corey Bryant | ba1c048 | 2012-08-14 16:43:43 -0400 | [diff] [blame] | 219 | static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets; |
Corey Bryant | efb87c1 | 2012-08-14 16:43:48 -0400 | [diff] [blame] | 220 | static int mon_refcount; |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 221 | |
Wayne Xia | 816f892 | 2011-10-12 11:32:41 +0800 | [diff] [blame] | 222 | static mon_cmd_t mon_cmds[]; |
| 223 | static mon_cmd_t info_cmds[]; |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 224 | |
Markus Armbruster | 8631b60 | 2010-02-18 11:41:55 +0100 | [diff] [blame] | 225 | Monitor *cur_mon; |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 226 | |
Alberto Garcia | dc59997 | 2016-03-10 13:55:26 +0200 | [diff] [blame] | 227 | static QEMUClockType event_clock_type = QEMU_CLOCK_REALTIME; |
| 228 | |
Stefan Hajnoczi | c60bf33 | 2013-11-14 11:54:14 +0100 | [diff] [blame] | 229 | static void monitor_command_cb(void *opaque, const char *cmdline, |
| 230 | void *readline_opaque); |
aliguori | 83ab795 | 2008-08-19 14:44:22 +0000 | [diff] [blame] | 231 | |
Markus Armbruster | 9f3982f | 2015-03-06 19:56:38 +0100 | [diff] [blame] | 232 | /** |
| 233 | * Is @mon a QMP monitor? |
| 234 | */ |
| 235 | static inline bool monitor_is_qmp(const Monitor *mon) |
Luiz Capitulino | 418173c | 2009-11-26 22:58:51 -0200 | [diff] [blame] | 236 | { |
| 237 | return (mon->flags & MONITOR_USE_CONTROL); |
| 238 | } |
| 239 | |
Markus Armbruster | 489653b | 2015-03-06 20:01:05 +0100 | [diff] [blame] | 240 | /** |
| 241 | * Is the current monitor, if any, a QMP monitor? |
| 242 | */ |
| 243 | bool monitor_cur_is_qmp(void) |
Markus Armbruster | 6620d3c | 2010-02-11 17:05:43 +0100 | [diff] [blame] | 244 | { |
Markus Armbruster | 9f3982f | 2015-03-06 19:56:38 +0100 | [diff] [blame] | 245 | return cur_mon && monitor_is_qmp(cur_mon); |
Markus Armbruster | 6620d3c | 2010-02-11 17:05:43 +0100 | [diff] [blame] | 246 | } |
| 247 | |
Anthony Liguori | 7060b47 | 2011-09-02 12:34:50 -0500 | [diff] [blame] | 248 | void monitor_read_command(Monitor *mon, int show_prompt) |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 249 | { |
Luiz Capitulino | 183e6e5 | 2009-12-14 18:53:23 -0200 | [diff] [blame] | 250 | if (!mon->rs) |
| 251 | return; |
| 252 | |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 253 | readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL); |
| 254 | if (show_prompt) |
| 255 | readline_show_prompt(mon->rs); |
| 256 | } |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 257 | |
Anthony Liguori | 7060b47 | 2011-09-02 12:34:50 -0500 | [diff] [blame] | 258 | int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func, |
| 259 | void *opaque) |
aliguori | bb5fc20 | 2009-03-05 23:01:15 +0000 | [diff] [blame] | 260 | { |
Markus Armbruster | bcf5d19 | 2015-03-12 17:26:46 +0100 | [diff] [blame] | 261 | if (mon->rs) { |
aliguori | cde76ee | 2009-03-05 23:01:51 +0000 | [diff] [blame] | 262 | readline_start(mon->rs, "Password: ", 1, readline_func, opaque); |
| 263 | /* prompt is printed on return from the command handler */ |
| 264 | return 0; |
| 265 | } else { |
| 266 | monitor_printf(mon, "terminal does not support password prompting\n"); |
| 267 | return -ENOTTY; |
| 268 | } |
aliguori | bb5fc20 | 2009-03-05 23:01:15 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Paolo Bonzini | 6cff3e8 | 2014-06-18 08:43:59 +0200 | [diff] [blame] | 271 | static void monitor_flush_locked(Monitor *mon); |
| 272 | |
Gerd Hoffmann | f628926 | 2013-03-19 10:57:56 +0100 | [diff] [blame] | 273 | static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond, |
| 274 | void *opaque) |
| 275 | { |
Laszlo Ersek | 293d2a0 | 2013-07-16 20:19:41 +0200 | [diff] [blame] | 276 | Monitor *mon = opaque; |
| 277 | |
Paolo Bonzini | 6cff3e8 | 2014-06-18 08:43:59 +0200 | [diff] [blame] | 278 | qemu_mutex_lock(&mon->out_lock); |
| 279 | mon->out_watch = 0; |
| 280 | monitor_flush_locked(mon); |
| 281 | qemu_mutex_unlock(&mon->out_lock); |
Gerd Hoffmann | f628926 | 2013-03-19 10:57:56 +0100 | [diff] [blame] | 282 | return FALSE; |
| 283 | } |
| 284 | |
Paolo Bonzini | 6cff3e8 | 2014-06-18 08:43:59 +0200 | [diff] [blame] | 285 | /* Called with mon->out_lock held. */ |
| 286 | static void monitor_flush_locked(Monitor *mon) |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 287 | { |
Gerd Hoffmann | f628926 | 2013-03-19 10:57:56 +0100 | [diff] [blame] | 288 | int rc; |
Luiz Capitulino | e1f2641 | 2013-03-25 13:52:26 -0400 | [diff] [blame] | 289 | size_t len; |
| 290 | const char *buf; |
Gerd Hoffmann | f628926 | 2013-03-19 10:57:56 +0100 | [diff] [blame] | 291 | |
Luiz Capitulino | 48c043d | 2013-04-02 15:07:33 -0400 | [diff] [blame] | 292 | if (mon->skip_flush) { |
| 293 | return; |
| 294 | } |
| 295 | |
Luiz Capitulino | e1f2641 | 2013-03-25 13:52:26 -0400 | [diff] [blame] | 296 | buf = qstring_get_str(mon->outbuf); |
| 297 | len = qstring_get_length(mon->outbuf); |
| 298 | |
Paolo Bonzini | a4cc73d | 2013-05-31 14:00:27 +0200 | [diff] [blame] | 299 | if (len && !mon->mux_out) { |
Luiz Capitulino | e1f2641 | 2013-03-25 13:52:26 -0400 | [diff] [blame] | 300 | rc = qemu_chr_fe_write(mon->chr, (const uint8_t *) buf, len); |
Stratos Psomadakis | 056f49f | 2014-01-27 12:30:15 +0200 | [diff] [blame] | 301 | if ((rc < 0 && errno != EAGAIN) || (rc == len)) { |
| 302 | /* all flushed or error */ |
Luiz Capitulino | e1f2641 | 2013-03-25 13:52:26 -0400 | [diff] [blame] | 303 | QDECREF(mon->outbuf); |
| 304 | mon->outbuf = qstring_new(); |
Gerd Hoffmann | f628926 | 2013-03-19 10:57:56 +0100 | [diff] [blame] | 305 | return; |
| 306 | } |
| 307 | if (rc > 0) { |
Eric Blake | 3b7c78c | 2016-05-17 16:00:15 -0600 | [diff] [blame] | 308 | /* partial write */ |
Luiz Capitulino | e1f2641 | 2013-03-25 13:52:26 -0400 | [diff] [blame] | 309 | QString *tmp = qstring_from_str(buf + rc); |
| 310 | QDECREF(mon->outbuf); |
| 311 | mon->outbuf = tmp; |
Gerd Hoffmann | f628926 | 2013-03-19 10:57:56 +0100 | [diff] [blame] | 312 | } |
Paolo Bonzini | 6cff3e8 | 2014-06-18 08:43:59 +0200 | [diff] [blame] | 313 | if (mon->out_watch == 0) { |
Roger Pau Monne | e02bc6d | 2014-05-23 17:57:49 +0200 | [diff] [blame] | 314 | mon->out_watch = qemu_chr_fe_add_watch(mon->chr, G_IO_OUT|G_IO_HUP, |
Paolo Bonzini | 6cff3e8 | 2014-06-18 08:43:59 +0200 | [diff] [blame] | 315 | monitor_unblocked, mon); |
Laszlo Ersek | 293d2a0 | 2013-07-16 20:19:41 +0200 | [diff] [blame] | 316 | } |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | |
Paolo Bonzini | 6cff3e8 | 2014-06-18 08:43:59 +0200 | [diff] [blame] | 320 | void monitor_flush(Monitor *mon) |
| 321 | { |
| 322 | qemu_mutex_lock(&mon->out_lock); |
| 323 | monitor_flush_locked(mon); |
| 324 | qemu_mutex_unlock(&mon->out_lock); |
| 325 | } |
| 326 | |
Luiz Capitulino | e1f2641 | 2013-03-25 13:52:26 -0400 | [diff] [blame] | 327 | /* flush at every end of line */ |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 328 | static void monitor_puts(Monitor *mon, const char *str) |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 329 | { |
ths | 60fe76f | 2007-12-16 03:02:09 +0000 | [diff] [blame] | 330 | char c; |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 331 | |
Paolo Bonzini | 6cff3e8 | 2014-06-18 08:43:59 +0200 | [diff] [blame] | 332 | qemu_mutex_lock(&mon->out_lock); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 333 | for(;;) { |
| 334 | c = *str++; |
| 335 | if (c == '\0') |
| 336 | break; |
Luiz Capitulino | e1f2641 | 2013-03-25 13:52:26 -0400 | [diff] [blame] | 337 | if (c == '\n') { |
| 338 | qstring_append_chr(mon->outbuf, '\r'); |
| 339 | } |
| 340 | qstring_append_chr(mon->outbuf, c); |
| 341 | if (c == '\n') { |
Paolo Bonzini | 6cff3e8 | 2014-06-18 08:43:59 +0200 | [diff] [blame] | 342 | monitor_flush_locked(mon); |
Luiz Capitulino | e1f2641 | 2013-03-25 13:52:26 -0400 | [diff] [blame] | 343 | } |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 344 | } |
Paolo Bonzini | 6cff3e8 | 2014-06-18 08:43:59 +0200 | [diff] [blame] | 345 | qemu_mutex_unlock(&mon->out_lock); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 346 | } |
| 347 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 348 | void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap) |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 349 | { |
Luiz Capitulino | e1f2641 | 2013-03-25 13:52:26 -0400 | [diff] [blame] | 350 | char *buf; |
Luiz Capitulino | b8b0826 | 2010-02-10 23:50:04 -0200 | [diff] [blame] | 351 | |
Luiz Capitulino | 2daa119 | 2009-12-14 18:53:24 -0200 | [diff] [blame] | 352 | if (!mon) |
| 353 | return; |
| 354 | |
Markus Armbruster | 9f3982f | 2015-03-06 19:56:38 +0100 | [diff] [blame] | 355 | if (monitor_is_qmp(mon)) { |
Luiz Capitulino | b8b0826 | 2010-02-10 23:50:04 -0200 | [diff] [blame] | 356 | return; |
Luiz Capitulino | 4a29a85 | 2009-11-26 22:59:05 -0200 | [diff] [blame] | 357 | } |
Luiz Capitulino | b8b0826 | 2010-02-10 23:50:04 -0200 | [diff] [blame] | 358 | |
Luiz Capitulino | e1f2641 | 2013-03-25 13:52:26 -0400 | [diff] [blame] | 359 | buf = g_strdup_vprintf(fmt, ap); |
Luiz Capitulino | b8b0826 | 2010-02-10 23:50:04 -0200 | [diff] [blame] | 360 | monitor_puts(mon, buf); |
Luiz Capitulino | e1f2641 | 2013-03-25 13:52:26 -0400 | [diff] [blame] | 361 | g_free(buf); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 362 | } |
| 363 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 364 | void monitor_printf(Monitor *mon, const char *fmt, ...) |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 365 | { |
| 366 | va_list ap; |
| 367 | va_start(ap, fmt); |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 368 | monitor_vprintf(mon, fmt, ap); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 369 | va_end(ap); |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Pavel Butsykin | caf1531 | 2015-09-22 16:18:17 +0300 | [diff] [blame] | 372 | int monitor_fprintf(FILE *stream, const char *fmt, ...) |
bellard | 7fe4848 | 2004-10-09 18:08:01 +0000 | [diff] [blame] | 373 | { |
| 374 | va_list ap; |
| 375 | va_start(ap, fmt); |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 376 | monitor_vprintf((Monitor *)stream, fmt, ap); |
bellard | 7fe4848 | 2004-10-09 18:08:01 +0000 | [diff] [blame] | 377 | va_end(ap); |
| 378 | return 0; |
| 379 | } |
| 380 | |
Luiz Capitulino | 9b57c02 | 2009-11-26 22:58:58 -0200 | [diff] [blame] | 381 | static void monitor_json_emitter(Monitor *mon, const QObject *data) |
| 382 | { |
| 383 | QString *json; |
| 384 | |
Luiz Capitulino | 83a27d4 | 2010-11-22 17:10:37 -0200 | [diff] [blame] | 385 | json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) : |
| 386 | qobject_to_json(data); |
Luiz Capitulino | 9b57c02 | 2009-11-26 22:58:58 -0200 | [diff] [blame] | 387 | assert(json != NULL); |
| 388 | |
Luiz Capitulino | b8b0826 | 2010-02-10 23:50:04 -0200 | [diff] [blame] | 389 | qstring_append_chr(json, '\n'); |
| 390 | monitor_puts(mon, qstring_get_str(json)); |
Luiz Capitulino | 4a29a85 | 2009-11-26 22:59:05 -0200 | [diff] [blame] | 391 | |
Luiz Capitulino | 9b57c02 | 2009-11-26 22:58:58 -0200 | [diff] [blame] | 392 | QDECREF(json); |
| 393 | } |
| 394 | |
Eric Blake | 7fb1cf1 | 2015-11-18 01:52:57 -0700 | [diff] [blame] | 395 | static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = { |
Markus Armbruster | b9b03ab | 2015-10-15 17:08:33 +0200 | [diff] [blame] | 396 | /* Limit guest-triggerable events to 1 per second */ |
| 397 | [QAPI_EVENT_RTC_CHANGE] = { 1000 * SCALE_MS }, |
| 398 | [QAPI_EVENT_WATCHDOG] = { 1000 * SCALE_MS }, |
| 399 | [QAPI_EVENT_BALLOON_CHANGE] = { 1000 * SCALE_MS }, |
| 400 | [QAPI_EVENT_QUORUM_REPORT_BAD] = { 1000 * SCALE_MS }, |
| 401 | [QAPI_EVENT_QUORUM_FAILURE] = { 1000 * SCALE_MS }, |
| 402 | [QAPI_EVENT_VSERPORT_CHANGE] = { 1000 * SCALE_MS }, |
| 403 | }; |
| 404 | |
Markus Armbruster | a24712a | 2015-10-15 17:08:34 +0200 | [diff] [blame] | 405 | GHashTable *monitor_qapi_event_state; |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 406 | |
| 407 | /* |
Wenchao Xia | 43a14cf | 2014-06-18 08:43:31 +0200 | [diff] [blame] | 408 | * Emits the event to every monitor instance, @event is only used for trace |
Paolo Bonzini | d622cb5 | 2014-06-18 08:44:00 +0200 | [diff] [blame] | 409 | * Called with monitor_lock held. |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 410 | */ |
Markus Armbruster | 688b4b7 | 2015-10-15 17:08:30 +0200 | [diff] [blame] | 411 | static void monitor_qapi_event_emit(QAPIEvent event, QDict *qdict) |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 412 | { |
| 413 | Monitor *mon; |
| 414 | |
Markus Armbruster | 688b4b7 | 2015-10-15 17:08:30 +0200 | [diff] [blame] | 415 | trace_monitor_protocol_event_emit(event, qdict); |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 416 | QLIST_FOREACH(mon, &mon_list, entry) { |
Markus Armbruster | 9f3982f | 2015-03-06 19:56:38 +0100 | [diff] [blame] | 417 | if (monitor_is_qmp(mon) && mon->qmp.in_command_mode) { |
Markus Armbruster | 688b4b7 | 2015-10-15 17:08:30 +0200 | [diff] [blame] | 418 | monitor_json_emitter(mon, QOBJECT(qdict)); |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | } |
| 422 | |
Markus Armbruster | a24712a | 2015-10-15 17:08:34 +0200 | [diff] [blame] | 423 | static void monitor_qapi_event_handler(void *opaque); |
| 424 | |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 425 | /* |
| 426 | * Queue a new event for emission to Monitor instances, |
| 427 | * applying any rate limiting if required. |
| 428 | */ |
| 429 | static void |
Markus Armbruster | 688b4b7 | 2015-10-15 17:08:30 +0200 | [diff] [blame] | 430 | monitor_qapi_event_queue(QAPIEvent event, QDict *qdict, Error **errp) |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 431 | { |
Markus Armbruster | b9b03ab | 2015-10-15 17:08:33 +0200 | [diff] [blame] | 432 | MonitorQAPIEventConf *evconf; |
Wenchao Xia | 43a14cf | 2014-06-18 08:43:31 +0200 | [diff] [blame] | 433 | MonitorQAPIEventState *evstate; |
Markus Armbruster | 93f8f98 | 2015-10-15 17:08:31 +0200 | [diff] [blame] | 434 | |
Eric Blake | 7fb1cf1 | 2015-11-18 01:52:57 -0700 | [diff] [blame] | 435 | assert(event < QAPI_EVENT__MAX); |
Markus Armbruster | b9b03ab | 2015-10-15 17:08:33 +0200 | [diff] [blame] | 436 | evconf = &monitor_qapi_event_conf[event]; |
Markus Armbruster | b9b03ab | 2015-10-15 17:08:33 +0200 | [diff] [blame] | 437 | trace_monitor_protocol_event_queue(event, qdict, evconf->rate); |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 438 | |
Paolo Bonzini | d622cb5 | 2014-06-18 08:44:00 +0200 | [diff] [blame] | 439 | qemu_mutex_lock(&monitor_lock); |
Markus Armbruster | 93f8f98 | 2015-10-15 17:08:31 +0200 | [diff] [blame] | 440 | |
Markus Armbruster | b9b03ab | 2015-10-15 17:08:33 +0200 | [diff] [blame] | 441 | if (!evconf->rate) { |
Markus Armbruster | 93f8f98 | 2015-10-15 17:08:31 +0200 | [diff] [blame] | 442 | /* Unthrottled event */ |
Markus Armbruster | 688b4b7 | 2015-10-15 17:08:30 +0200 | [diff] [blame] | 443 | monitor_qapi_event_emit(event, qdict); |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 444 | } else { |
Markus Armbruster | 7de0be6 | 2015-10-15 17:08:35 +0200 | [diff] [blame] | 445 | QDict *data = qobject_to_qdict(qdict_get(qdict, "data")); |
| 446 | MonitorQAPIEventState key = { .event = event, .data = data }; |
Markus Armbruster | a24712a | 2015-10-15 17:08:34 +0200 | [diff] [blame] | 447 | |
| 448 | evstate = g_hash_table_lookup(monitor_qapi_event_state, &key); |
| 449 | assert(!evstate || timer_pending(evstate->timer)); |
| 450 | |
| 451 | if (evstate) { |
Markus Armbruster | 93f8f98 | 2015-10-15 17:08:31 +0200 | [diff] [blame] | 452 | /* |
Markus Armbruster | b9b03ab | 2015-10-15 17:08:33 +0200 | [diff] [blame] | 453 | * Timer is pending for (at least) evconf->rate ns after |
Markus Armbruster | 93f8f98 | 2015-10-15 17:08:31 +0200 | [diff] [blame] | 454 | * last send. Store event for sending when timer fires, |
| 455 | * replacing a prior stored event if any. |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 456 | */ |
Markus Armbruster | 93f8f98 | 2015-10-15 17:08:31 +0200 | [diff] [blame] | 457 | QDECREF(evstate->qdict); |
Markus Armbruster | 688b4b7 | 2015-10-15 17:08:30 +0200 | [diff] [blame] | 458 | evstate->qdict = qdict; |
| 459 | QINCREF(evstate->qdict); |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 460 | } else { |
Markus Armbruster | 93f8f98 | 2015-10-15 17:08:31 +0200 | [diff] [blame] | 461 | /* |
Markus Armbruster | b9b03ab | 2015-10-15 17:08:33 +0200 | [diff] [blame] | 462 | * Last send was (at least) evconf->rate ns ago. |
Markus Armbruster | 93f8f98 | 2015-10-15 17:08:31 +0200 | [diff] [blame] | 463 | * Send immediately, and arm the timer to call |
Markus Armbruster | b9b03ab | 2015-10-15 17:08:33 +0200 | [diff] [blame] | 464 | * monitor_qapi_event_handler() in evconf->rate ns. Any |
Markus Armbruster | 93f8f98 | 2015-10-15 17:08:31 +0200 | [diff] [blame] | 465 | * events arriving before then will be delayed until then. |
| 466 | */ |
Alberto Garcia | dc59997 | 2016-03-10 13:55:26 +0200 | [diff] [blame] | 467 | int64_t now = qemu_clock_get_ns(event_clock_type); |
Markus Armbruster | 93f8f98 | 2015-10-15 17:08:31 +0200 | [diff] [blame] | 468 | |
Markus Armbruster | 688b4b7 | 2015-10-15 17:08:30 +0200 | [diff] [blame] | 469 | monitor_qapi_event_emit(event, qdict); |
Markus Armbruster | a24712a | 2015-10-15 17:08:34 +0200 | [diff] [blame] | 470 | |
| 471 | evstate = g_new(MonitorQAPIEventState, 1); |
| 472 | evstate->event = event; |
Markus Armbruster | 7de0be6 | 2015-10-15 17:08:35 +0200 | [diff] [blame] | 473 | evstate->data = data; |
| 474 | QINCREF(evstate->data); |
Markus Armbruster | a24712a | 2015-10-15 17:08:34 +0200 | [diff] [blame] | 475 | evstate->qdict = NULL; |
Alberto Garcia | dc59997 | 2016-03-10 13:55:26 +0200 | [diff] [blame] | 476 | evstate->timer = timer_new_ns(event_clock_type, |
Markus Armbruster | a24712a | 2015-10-15 17:08:34 +0200 | [diff] [blame] | 477 | monitor_qapi_event_handler, |
| 478 | evstate); |
| 479 | g_hash_table_add(monitor_qapi_event_state, evstate); |
Markus Armbruster | b9b03ab | 2015-10-15 17:08:33 +0200 | [diff] [blame] | 480 | timer_mod_ns(evstate->timer, now + evconf->rate); |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 481 | } |
| 482 | } |
Markus Armbruster | 93f8f98 | 2015-10-15 17:08:31 +0200 | [diff] [blame] | 483 | |
Paolo Bonzini | d622cb5 | 2014-06-18 08:44:00 +0200 | [diff] [blame] | 484 | qemu_mutex_unlock(&monitor_lock); |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 485 | } |
| 486 | |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 487 | /* |
Markus Armbruster | b9b03ab | 2015-10-15 17:08:33 +0200 | [diff] [blame] | 488 | * This function runs evconf->rate ns after sending a throttled |
Markus Armbruster | 93f8f98 | 2015-10-15 17:08:31 +0200 | [diff] [blame] | 489 | * event. |
| 490 | * If another event has since been stored, send it. |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 491 | */ |
Wenchao Xia | 43a14cf | 2014-06-18 08:43:31 +0200 | [diff] [blame] | 492 | static void monitor_qapi_event_handler(void *opaque) |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 493 | { |
Wenchao Xia | 43a14cf | 2014-06-18 08:43:31 +0200 | [diff] [blame] | 494 | MonitorQAPIEventState *evstate = opaque; |
Markus Armbruster | b9b03ab | 2015-10-15 17:08:33 +0200 | [diff] [blame] | 495 | MonitorQAPIEventConf *evconf = &monitor_qapi_event_conf[evstate->event]; |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 496 | |
Markus Armbruster | 93f8f98 | 2015-10-15 17:08:31 +0200 | [diff] [blame] | 497 | trace_monitor_protocol_event_handler(evstate->event, evstate->qdict); |
Paolo Bonzini | d622cb5 | 2014-06-18 08:44:00 +0200 | [diff] [blame] | 498 | qemu_mutex_lock(&monitor_lock); |
Markus Armbruster | 93f8f98 | 2015-10-15 17:08:31 +0200 | [diff] [blame] | 499 | |
Markus Armbruster | 688b4b7 | 2015-10-15 17:08:30 +0200 | [diff] [blame] | 500 | if (evstate->qdict) { |
Alberto Garcia | dc59997 | 2016-03-10 13:55:26 +0200 | [diff] [blame] | 501 | int64_t now = qemu_clock_get_ns(event_clock_type); |
Markus Armbruster | 93f8f98 | 2015-10-15 17:08:31 +0200 | [diff] [blame] | 502 | |
Markus Armbruster | 688b4b7 | 2015-10-15 17:08:30 +0200 | [diff] [blame] | 503 | monitor_qapi_event_emit(evstate->event, evstate->qdict); |
| 504 | QDECREF(evstate->qdict); |
| 505 | evstate->qdict = NULL; |
Markus Armbruster | b9b03ab | 2015-10-15 17:08:33 +0200 | [diff] [blame] | 506 | timer_mod_ns(evstate->timer, now + evconf->rate); |
Markus Armbruster | a24712a | 2015-10-15 17:08:34 +0200 | [diff] [blame] | 507 | } else { |
| 508 | g_hash_table_remove(monitor_qapi_event_state, evstate); |
Markus Armbruster | 7de0be6 | 2015-10-15 17:08:35 +0200 | [diff] [blame] | 509 | QDECREF(evstate->data); |
Markus Armbruster | a24712a | 2015-10-15 17:08:34 +0200 | [diff] [blame] | 510 | timer_free(evstate->timer); |
| 511 | g_free(evstate); |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 512 | } |
Markus Armbruster | 93f8f98 | 2015-10-15 17:08:31 +0200 | [diff] [blame] | 513 | |
Paolo Bonzini | d622cb5 | 2014-06-18 08:44:00 +0200 | [diff] [blame] | 514 | qemu_mutex_unlock(&monitor_lock); |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 515 | } |
| 516 | |
Markus Armbruster | a24712a | 2015-10-15 17:08:34 +0200 | [diff] [blame] | 517 | static unsigned int qapi_event_throttle_hash(const void *key) |
| 518 | { |
| 519 | const MonitorQAPIEventState *evstate = key; |
Markus Armbruster | 7de0be6 | 2015-10-15 17:08:35 +0200 | [diff] [blame] | 520 | unsigned int hash = evstate->event * 255; |
Markus Armbruster | a24712a | 2015-10-15 17:08:34 +0200 | [diff] [blame] | 521 | |
Markus Armbruster | 7de0be6 | 2015-10-15 17:08:35 +0200 | [diff] [blame] | 522 | if (evstate->event == QAPI_EVENT_VSERPORT_CHANGE) { |
| 523 | hash += g_str_hash(qdict_get_str(evstate->data, "id")); |
| 524 | } |
| 525 | |
Alberto Garcia | 6d425eb | 2016-03-10 13:55:25 +0200 | [diff] [blame] | 526 | if (evstate->event == QAPI_EVENT_QUORUM_REPORT_BAD) { |
| 527 | hash += g_str_hash(qdict_get_str(evstate->data, "node-name")); |
| 528 | } |
| 529 | |
Markus Armbruster | 7de0be6 | 2015-10-15 17:08:35 +0200 | [diff] [blame] | 530 | return hash; |
Markus Armbruster | a24712a | 2015-10-15 17:08:34 +0200 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | static gboolean qapi_event_throttle_equal(const void *a, const void *b) |
| 534 | { |
| 535 | const MonitorQAPIEventState *eva = a; |
| 536 | const MonitorQAPIEventState *evb = b; |
| 537 | |
Markus Armbruster | 7de0be6 | 2015-10-15 17:08:35 +0200 | [diff] [blame] | 538 | if (eva->event != evb->event) { |
| 539 | return FALSE; |
| 540 | } |
| 541 | |
| 542 | if (eva->event == QAPI_EVENT_VSERPORT_CHANGE) { |
| 543 | return !strcmp(qdict_get_str(eva->data, "id"), |
| 544 | qdict_get_str(evb->data, "id")); |
| 545 | } |
| 546 | |
Alberto Garcia | 6d425eb | 2016-03-10 13:55:25 +0200 | [diff] [blame] | 547 | if (eva->event == QAPI_EVENT_QUORUM_REPORT_BAD) { |
| 548 | return !strcmp(qdict_get_str(eva->data, "node-name"), |
| 549 | qdict_get_str(evb->data, "node-name")); |
| 550 | } |
| 551 | |
Markus Armbruster | 7de0be6 | 2015-10-15 17:08:35 +0200 | [diff] [blame] | 552 | return TRUE; |
Markus Armbruster | a24712a | 2015-10-15 17:08:34 +0200 | [diff] [blame] | 553 | } |
| 554 | |
Wenchao Xia | 43a14cf | 2014-06-18 08:43:31 +0200 | [diff] [blame] | 555 | static void monitor_qapi_event_init(void) |
Daniel P. Berrange | afeecec | 2012-06-14 18:12:57 +0100 | [diff] [blame] | 556 | { |
Alberto Garcia | dc59997 | 2016-03-10 13:55:26 +0200 | [diff] [blame] | 557 | if (qtest_enabled()) { |
| 558 | event_clock_type = QEMU_CLOCK_VIRTUAL; |
| 559 | } |
| 560 | |
Markus Armbruster | a24712a | 2015-10-15 17:08:34 +0200 | [diff] [blame] | 561 | monitor_qapi_event_state = g_hash_table_new(qapi_event_throttle_hash, |
| 562 | qapi_event_throttle_equal); |
Wenchao Xia | 43a14cf | 2014-06-18 08:43:31 +0200 | [diff] [blame] | 563 | qmp_event_set_func_emit(monitor_qapi_event_queue); |
Luiz Capitulino | 0d1ea87 | 2009-11-26 22:59:03 -0200 | [diff] [blame] | 564 | } |
| 565 | |
Marc-André Lureau | 119ebac | 2016-09-12 13:18:57 +0400 | [diff] [blame] | 566 | void qmp_qmp_capabilities(Error **errp) |
Luiz Capitulino | 4a7e119 | 2010-02-04 18:10:05 -0200 | [diff] [blame] | 567 | { |
Markus Armbruster | 485febc | 2015-03-13 17:25:50 +0100 | [diff] [blame] | 568 | cur_mon->qmp.in_command_mode = true; |
Luiz Capitulino | 4a7e119 | 2010-02-04 18:10:05 -0200 | [diff] [blame] | 569 | } |
| 570 | |
Markus Armbruster | 7ef6cf6 | 2015-03-06 19:12:36 +0100 | [diff] [blame] | 571 | static void handle_hmp_command(Monitor *mon, const char *cmdline); |
Luiz Capitulino | 0268d97 | 2010-10-22 10:08:02 -0200 | [diff] [blame] | 572 | |
Wenchao Xia | b01fe89 | 2013-08-27 20:38:19 +0800 | [diff] [blame] | 573 | static void monitor_data_init(Monitor *mon) |
| 574 | { |
| 575 | memset(mon, 0, sizeof(Monitor)); |
Paolo Bonzini | 6cff3e8 | 2014-06-18 08:43:59 +0200 | [diff] [blame] | 576 | qemu_mutex_init(&mon->out_lock); |
Wenchao Xia | b01fe89 | 2013-08-27 20:38:19 +0800 | [diff] [blame] | 577 | mon->outbuf = qstring_new(); |
Wenchao Xia | 7717239 | 2013-08-27 20:38:20 +0800 | [diff] [blame] | 578 | /* Use *mon_cmds by default. */ |
| 579 | mon->cmd_table = mon_cmds; |
Wenchao Xia | b01fe89 | 2013-08-27 20:38:19 +0800 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | static void monitor_data_destroy(Monitor *mon) |
| 583 | { |
Marc-André Lureau | 2ef4571 | 2016-08-01 15:23:42 +0400 | [diff] [blame] | 584 | if (mon->chr) { |
| 585 | qemu_chr_add_handlers(mon->chr, NULL, NULL, NULL, NULL); |
| 586 | } |
| 587 | if (monitor_is_qmp(mon)) { |
| 588 | json_message_parser_destroy(&mon->qmp.parser); |
| 589 | } |
| 590 | g_free(mon->rs); |
Wenchao Xia | b01fe89 | 2013-08-27 20:38:19 +0800 | [diff] [blame] | 591 | QDECREF(mon->outbuf); |
Paolo Bonzini | 6cff3e8 | 2014-06-18 08:43:59 +0200 | [diff] [blame] | 592 | qemu_mutex_destroy(&mon->out_lock); |
Wenchao Xia | b01fe89 | 2013-08-27 20:38:19 +0800 | [diff] [blame] | 593 | } |
| 594 | |
Luiz Capitulino | d51a67b | 2011-11-25 17:52:45 -0200 | [diff] [blame] | 595 | char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index, |
| 596 | int64_t cpu_index, Error **errp) |
Luiz Capitulino | 0268d97 | 2010-10-22 10:08:02 -0200 | [diff] [blame] | 597 | { |
Luiz Capitulino | d51a67b | 2011-11-25 17:52:45 -0200 | [diff] [blame] | 598 | char *output = NULL; |
Luiz Capitulino | 0268d97 | 2010-10-22 10:08:02 -0200 | [diff] [blame] | 599 | Monitor *old_mon, hmp; |
Luiz Capitulino | 0268d97 | 2010-10-22 10:08:02 -0200 | [diff] [blame] | 600 | |
Wenchao Xia | b01fe89 | 2013-08-27 20:38:19 +0800 | [diff] [blame] | 601 | monitor_data_init(&hmp); |
Luiz Capitulino | 48c043d | 2013-04-02 15:07:33 -0400 | [diff] [blame] | 602 | hmp.skip_flush = true; |
Luiz Capitulino | 0268d97 | 2010-10-22 10:08:02 -0200 | [diff] [blame] | 603 | |
| 604 | old_mon = cur_mon; |
| 605 | cur_mon = &hmp; |
| 606 | |
Luiz Capitulino | d51a67b | 2011-11-25 17:52:45 -0200 | [diff] [blame] | 607 | if (has_cpu_index) { |
| 608 | int ret = monitor_set_cpu(cpu_index); |
Luiz Capitulino | 0268d97 | 2010-10-22 10:08:02 -0200 | [diff] [blame] | 609 | if (ret < 0) { |
| 610 | cur_mon = old_mon; |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 611 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index", |
| 612 | "a CPU number"); |
Luiz Capitulino | 0268d97 | 2010-10-22 10:08:02 -0200 | [diff] [blame] | 613 | goto out; |
| 614 | } |
| 615 | } |
| 616 | |
Markus Armbruster | 7ef6cf6 | 2015-03-06 19:12:36 +0100 | [diff] [blame] | 617 | handle_hmp_command(&hmp, command_line); |
Luiz Capitulino | 0268d97 | 2010-10-22 10:08:02 -0200 | [diff] [blame] | 618 | cur_mon = old_mon; |
| 619 | |
Paolo Bonzini | 6cff3e8 | 2014-06-18 08:43:59 +0200 | [diff] [blame] | 620 | qemu_mutex_lock(&hmp.out_lock); |
Luiz Capitulino | 48c043d | 2013-04-02 15:07:33 -0400 | [diff] [blame] | 621 | if (qstring_get_length(hmp.outbuf) > 0) { |
| 622 | output = g_strdup(qstring_get_str(hmp.outbuf)); |
Luiz Capitulino | d51a67b | 2011-11-25 17:52:45 -0200 | [diff] [blame] | 623 | } else { |
| 624 | output = g_strdup(""); |
Luiz Capitulino | 0268d97 | 2010-10-22 10:08:02 -0200 | [diff] [blame] | 625 | } |
Paolo Bonzini | 6cff3e8 | 2014-06-18 08:43:59 +0200 | [diff] [blame] | 626 | qemu_mutex_unlock(&hmp.out_lock); |
Luiz Capitulino | 0268d97 | 2010-10-22 10:08:02 -0200 | [diff] [blame] | 627 | |
| 628 | out: |
Wenchao Xia | b01fe89 | 2013-08-27 20:38:19 +0800 | [diff] [blame] | 629 | monitor_data_destroy(&hmp); |
Luiz Capitulino | d51a67b | 2011-11-25 17:52:45 -0200 | [diff] [blame] | 630 | return output; |
Luiz Capitulino | 0268d97 | 2010-10-22 10:08:02 -0200 | [diff] [blame] | 631 | } |
| 632 | |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 633 | static int compare_cmd(const char *name, const char *list) |
| 634 | { |
| 635 | const char *p, *pstart; |
| 636 | int len; |
| 637 | len = strlen(name); |
| 638 | p = list; |
| 639 | for(;;) { |
| 640 | pstart = p; |
| 641 | p = strchr(p, '|'); |
| 642 | if (!p) |
| 643 | p = pstart + strlen(pstart); |
| 644 | if ((p - pstart) == len && !memcmp(pstart, name, len)) |
| 645 | return 1; |
| 646 | if (*p == '\0') |
| 647 | break; |
| 648 | p++; |
| 649 | } |
| 650 | return 0; |
| 651 | } |
| 652 | |
Wenchao Xia | f5438c0 | 2013-08-27 20:38:21 +0800 | [diff] [blame] | 653 | static int get_str(char *buf, int buf_size, const char **pp) |
| 654 | { |
| 655 | const char *p; |
| 656 | char *q; |
| 657 | int c; |
| 658 | |
| 659 | q = buf; |
| 660 | p = *pp; |
| 661 | while (qemu_isspace(*p)) { |
| 662 | p++; |
| 663 | } |
| 664 | if (*p == '\0') { |
| 665 | fail: |
| 666 | *q = '\0'; |
| 667 | *pp = p; |
| 668 | return -1; |
| 669 | } |
| 670 | if (*p == '\"') { |
| 671 | p++; |
| 672 | while (*p != '\0' && *p != '\"') { |
| 673 | if (*p == '\\') { |
| 674 | p++; |
| 675 | c = *p++; |
| 676 | switch (c) { |
| 677 | case 'n': |
| 678 | c = '\n'; |
| 679 | break; |
| 680 | case 'r': |
| 681 | c = '\r'; |
| 682 | break; |
| 683 | case '\\': |
| 684 | case '\'': |
| 685 | case '\"': |
| 686 | break; |
| 687 | default: |
Peter Maydell | 71baf78 | 2015-08-19 16:20:19 +0100 | [diff] [blame] | 688 | printf("unsupported escape code: '\\%c'\n", c); |
Wenchao Xia | f5438c0 | 2013-08-27 20:38:21 +0800 | [diff] [blame] | 689 | goto fail; |
| 690 | } |
| 691 | if ((q - buf) < buf_size - 1) { |
| 692 | *q++ = c; |
| 693 | } |
| 694 | } else { |
| 695 | if ((q - buf) < buf_size - 1) { |
| 696 | *q++ = *p; |
| 697 | } |
| 698 | p++; |
| 699 | } |
| 700 | } |
| 701 | if (*p != '\"') { |
Peter Maydell | 71baf78 | 2015-08-19 16:20:19 +0100 | [diff] [blame] | 702 | printf("unterminated string\n"); |
Wenchao Xia | f5438c0 | 2013-08-27 20:38:21 +0800 | [diff] [blame] | 703 | goto fail; |
| 704 | } |
| 705 | p++; |
| 706 | } else { |
| 707 | while (*p != '\0' && !qemu_isspace(*p)) { |
| 708 | if ((q - buf) < buf_size - 1) { |
| 709 | *q++ = *p; |
| 710 | } |
| 711 | p++; |
| 712 | } |
| 713 | } |
| 714 | *q = '\0'; |
| 715 | *pp = p; |
| 716 | return 0; |
| 717 | } |
| 718 | |
| 719 | #define MAX_ARGS 16 |
| 720 | |
Wenchao Xia | dcc70cd | 2013-08-27 20:38:22 +0800 | [diff] [blame] | 721 | static void free_cmdline_args(char **args, int nb_args) |
| 722 | { |
| 723 | int i; |
| 724 | |
| 725 | assert(nb_args <= MAX_ARGS); |
| 726 | |
| 727 | for (i = 0; i < nb_args; i++) { |
| 728 | g_free(args[i]); |
| 729 | } |
| 730 | |
| 731 | } |
| 732 | |
| 733 | /* |
| 734 | * Parse the command line to get valid args. |
| 735 | * @cmdline: command line to be parsed. |
| 736 | * @pnb_args: location to store the number of args, must NOT be NULL. |
| 737 | * @args: location to store the args, which should be freed by caller, must |
| 738 | * NOT be NULL. |
| 739 | * |
| 740 | * Returns 0 on success, negative on failure. |
| 741 | * |
| 742 | * NOTE: this parser is an approximate form of the real command parser. Number |
| 743 | * of args have a limit of MAX_ARGS. If cmdline contains more, it will |
| 744 | * return with failure. |
| 745 | */ |
| 746 | static int parse_cmdline(const char *cmdline, |
| 747 | int *pnb_args, char **args) |
Wenchao Xia | f5438c0 | 2013-08-27 20:38:21 +0800 | [diff] [blame] | 748 | { |
| 749 | const char *p; |
| 750 | int nb_args, ret; |
| 751 | char buf[1024]; |
| 752 | |
| 753 | p = cmdline; |
| 754 | nb_args = 0; |
| 755 | for (;;) { |
| 756 | while (qemu_isspace(*p)) { |
| 757 | p++; |
| 758 | } |
| 759 | if (*p == '\0') { |
| 760 | break; |
| 761 | } |
| 762 | if (nb_args >= MAX_ARGS) { |
Wenchao Xia | dcc70cd | 2013-08-27 20:38:22 +0800 | [diff] [blame] | 763 | goto fail; |
Wenchao Xia | f5438c0 | 2013-08-27 20:38:21 +0800 | [diff] [blame] | 764 | } |
| 765 | ret = get_str(buf, sizeof(buf), &p); |
Wenchao Xia | dcc70cd | 2013-08-27 20:38:22 +0800 | [diff] [blame] | 766 | if (ret < 0) { |
| 767 | goto fail; |
| 768 | } |
Wenchao Xia | f5438c0 | 2013-08-27 20:38:21 +0800 | [diff] [blame] | 769 | args[nb_args] = g_strdup(buf); |
| 770 | nb_args++; |
Wenchao Xia | f5438c0 | 2013-08-27 20:38:21 +0800 | [diff] [blame] | 771 | } |
| 772 | *pnb_args = nb_args; |
Wenchao Xia | dcc70cd | 2013-08-27 20:38:22 +0800 | [diff] [blame] | 773 | return 0; |
| 774 | |
| 775 | fail: |
| 776 | free_cmdline_args(args, nb_args); |
| 777 | return -1; |
Wenchao Xia | f5438c0 | 2013-08-27 20:38:21 +0800 | [diff] [blame] | 778 | } |
| 779 | |
Wenchao Xia | 6685549 | 2013-08-27 20:38:23 +0800 | [diff] [blame] | 780 | static void help_cmd_dump_one(Monitor *mon, |
| 781 | const mon_cmd_t *cmd, |
| 782 | char **prefix_args, |
| 783 | int prefix_args_nb) |
| 784 | { |
| 785 | int i; |
| 786 | |
| 787 | for (i = 0; i < prefix_args_nb; i++) { |
| 788 | monitor_printf(mon, "%s ", prefix_args[i]); |
| 789 | } |
| 790 | monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help); |
| 791 | } |
| 792 | |
| 793 | /* @args[@arg_index] is the valid command need to find in @cmds */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 794 | static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds, |
Wenchao Xia | 6685549 | 2013-08-27 20:38:23 +0800 | [diff] [blame] | 795 | char **args, int nb_args, int arg_index) |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 796 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 797 | const mon_cmd_t *cmd; |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 798 | |
Wenchao Xia | 6685549 | 2013-08-27 20:38:23 +0800 | [diff] [blame] | 799 | /* No valid arg need to compare with, dump all in *cmds */ |
| 800 | if (arg_index >= nb_args) { |
| 801 | for (cmd = cmds; cmd->name != NULL; cmd++) { |
| 802 | help_cmd_dump_one(mon, cmd, args, arg_index); |
| 803 | } |
| 804 | return; |
| 805 | } |
| 806 | |
| 807 | /* Find one entry to dump */ |
| 808 | for (cmd = cmds; cmd->name != NULL; cmd++) { |
| 809 | if (compare_cmd(args[arg_index], cmd->name)) { |
| 810 | if (cmd->sub_table) { |
| 811 | /* continue with next arg */ |
| 812 | help_cmd_dump(mon, cmd->sub_table, |
| 813 | args, nb_args, arg_index + 1); |
| 814 | } else { |
| 815 | help_cmd_dump_one(mon, cmd, args, arg_index); |
| 816 | } |
| 817 | break; |
| 818 | } |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 819 | } |
| 820 | } |
| 821 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 822 | static void help_cmd(Monitor *mon, const char *name) |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 823 | { |
Wenchao Xia | 6685549 | 2013-08-27 20:38:23 +0800 | [diff] [blame] | 824 | char *args[MAX_ARGS]; |
| 825 | int nb_args = 0; |
| 826 | |
| 827 | /* 1. parse user input */ |
| 828 | if (name) { |
| 829 | /* special case for log, directly dump and return */ |
| 830 | if (!strcmp(name, "log")) { |
Peter Maydell | 38dad9e | 2013-02-11 16:41:25 +0000 | [diff] [blame] | 831 | const QEMULogItem *item; |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 832 | monitor_printf(mon, "Log items (comma separated):\n"); |
| 833 | monitor_printf(mon, "%-10s %s\n", "none", "remove all logs"); |
Peter Maydell | 38dad9e | 2013-02-11 16:41:25 +0000 | [diff] [blame] | 834 | for (item = qemu_log_items; item->mask != 0; item++) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 835 | monitor_printf(mon, "%-10s %s\n", item->name, item->help); |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 836 | } |
Wenchao Xia | 6685549 | 2013-08-27 20:38:23 +0800 | [diff] [blame] | 837 | return; |
| 838 | } |
| 839 | |
| 840 | if (parse_cmdline(name, &nb_args, args) < 0) { |
| 841 | return; |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 842 | } |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 843 | } |
Wenchao Xia | 6685549 | 2013-08-27 20:38:23 +0800 | [diff] [blame] | 844 | |
| 845 | /* 2. dump the contents according to parsed args */ |
| 846 | help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0); |
| 847 | |
| 848 | free_cmdline_args(args, nb_args); |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 849 | } |
| 850 | |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 851 | static void do_help_cmd(Monitor *mon, const QDict *qdict) |
Luiz Capitulino | 3818318 | 2009-08-28 15:27:08 -0300 | [diff] [blame] | 852 | { |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 853 | help_cmd(mon, qdict_get_try_str(qdict, "name")); |
Luiz Capitulino | 3818318 | 2009-08-28 15:27:08 -0300 | [diff] [blame] | 854 | } |
| 855 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 856 | static void hmp_trace_event(Monitor *mon, const QDict *qdict) |
Prerna Saxena | 22890ab | 2010-06-24 17:04:53 +0530 | [diff] [blame] | 857 | { |
| 858 | const char *tp_name = qdict_get_str(qdict, "name"); |
| 859 | bool new_state = qdict_get_bool(qdict, "option"); |
Lluís Vilanova | 77e2b17 | 2016-07-11 12:53:57 +0200 | [diff] [blame] | 860 | bool has_vcpu = qdict_haskey(qdict, "vcpu"); |
| 861 | int vcpu = qdict_get_try_int(qdict, "vcpu", 0); |
Lluís Vilanova | 14101d0 | 2014-08-25 13:20:03 +0200 | [diff] [blame] | 862 | Error *local_err = NULL; |
Blue Swirl | f871d68 | 2010-10-13 19:14:29 +0000 | [diff] [blame] | 863 | |
Lluís Vilanova | 77e2b17 | 2016-07-11 12:53:57 +0200 | [diff] [blame] | 864 | if (vcpu < 0) { |
| 865 | monitor_printf(mon, "argument vcpu must be positive"); |
| 866 | return; |
| 867 | } |
| 868 | |
| 869 | 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] | 870 | if (local_err) { |
Markus Armbruster | 091e38b | 2015-02-10 15:15:43 +0100 | [diff] [blame] | 871 | error_report_err(local_err); |
Blue Swirl | f871d68 | 2010-10-13 19:14:29 +0000 | [diff] [blame] | 872 | } |
Prerna Saxena | 22890ab | 2010-06-24 17:04:53 +0530 | [diff] [blame] | 873 | } |
Stefan Hajnoczi | c5ceb52 | 2010-07-13 09:26:33 +0100 | [diff] [blame] | 874 | |
Michael Roth | c45a816 | 2011-10-02 08:44:37 -0500 | [diff] [blame] | 875 | #ifdef CONFIG_TRACE_SIMPLE |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 876 | static void hmp_trace_file(Monitor *mon, const QDict *qdict) |
Stefan Hajnoczi | c5ceb52 | 2010-07-13 09:26:33 +0100 | [diff] [blame] | 877 | { |
| 878 | const char *op = qdict_get_try_str(qdict, "op"); |
| 879 | const char *arg = qdict_get_try_str(qdict, "arg"); |
| 880 | |
| 881 | if (!op) { |
| 882 | st_print_trace_file_status((FILE *)mon, &monitor_fprintf); |
| 883 | } else if (!strcmp(op, "on")) { |
| 884 | st_set_trace_file_enabled(true); |
| 885 | } else if (!strcmp(op, "off")) { |
| 886 | st_set_trace_file_enabled(false); |
| 887 | } else if (!strcmp(op, "flush")) { |
| 888 | st_flush_trace_buffer(); |
| 889 | } else if (!strcmp(op, "set")) { |
| 890 | if (arg) { |
| 891 | st_set_trace_file(arg); |
| 892 | } |
| 893 | } else { |
| 894 | monitor_printf(mon, "unexpected argument \"%s\"\n", op); |
| 895 | help_cmd(mon, "trace-file"); |
| 896 | } |
| 897 | } |
Prerna Saxena | 22890ab | 2010-06-24 17:04:53 +0530 | [diff] [blame] | 898 | #endif |
| 899 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 900 | static void hmp_info_help(Monitor *mon, const QDict *qdict) |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 901 | { |
Luiz Capitulino | 13c7425 | 2009-10-07 13:41:55 -0300 | [diff] [blame] | 902 | help_cmd(mon, "info"); |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 903 | } |
| 904 | |
Marc-André Lureau | 9e812b6 | 2016-09-12 13:19:05 +0400 | [diff] [blame] | 905 | static void query_commands_cb(QmpCommand *cmd, void *opaque) |
bellard | 9bc9d1c | 2004-10-10 15:15:51 +0000 | [diff] [blame] | 906 | { |
Marc-André Lureau | 9e812b6 | 2016-09-12 13:19:05 +0400 | [diff] [blame] | 907 | CommandInfoList *info, **list = opaque; |
Luiz Capitulino | e3bba9d | 2009-11-26 22:58:56 -0200 | [diff] [blame] | 908 | |
Marc-André Lureau | 9e812b6 | 2016-09-12 13:19:05 +0400 | [diff] [blame] | 909 | if (!cmd->enabled) { |
| 910 | return; |
Luiz Capitulino | e3bba9d | 2009-11-26 22:58:56 -0200 | [diff] [blame] | 911 | } |
| 912 | |
Marc-André Lureau | 9e812b6 | 2016-09-12 13:19:05 +0400 | [diff] [blame] | 913 | info = g_malloc0(sizeof(*info)); |
| 914 | info->value = g_malloc0(sizeof(*info->value)); |
| 915 | info->value->name = g_strdup(cmd->name); |
| 916 | info->next = *list; |
| 917 | *list = info; |
| 918 | } |
| 919 | |
| 920 | CommandInfoList *qmp_query_commands(Error **errp) |
| 921 | { |
| 922 | CommandInfoList *list = NULL; |
| 923 | |
| 924 | qmp_for_each_command(query_commands_cb, &list); |
| 925 | |
| 926 | return list; |
ths | a36e69d | 2007-12-02 05:18:19 +0000 | [diff] [blame] | 927 | } |
| 928 | |
Daniel P. Berrange | 4860853 | 2012-05-21 17:59:51 +0100 | [diff] [blame] | 929 | EventInfoList *qmp_query_events(Error **errp) |
| 930 | { |
| 931 | EventInfoList *info, *ev_list = NULL; |
Wenchao Xia | 7517517 | 2014-06-18 08:43:54 +0200 | [diff] [blame] | 932 | QAPIEvent e; |
Daniel P. Berrange | 4860853 | 2012-05-21 17:59:51 +0100 | [diff] [blame] | 933 | |
Eric Blake | 7fb1cf1 | 2015-11-18 01:52:57 -0700 | [diff] [blame] | 934 | for (e = 0 ; e < QAPI_EVENT__MAX ; e++) { |
Wenchao Xia | 7517517 | 2014-06-18 08:43:54 +0200 | [diff] [blame] | 935 | const char *event_name = QAPIEvent_lookup[e]; |
Daniel P. Berrange | 4860853 | 2012-05-21 17:59:51 +0100 | [diff] [blame] | 936 | assert(event_name != NULL); |
| 937 | info = g_malloc0(sizeof(*info)); |
| 938 | info->value = g_malloc0(sizeof(*info->value)); |
| 939 | info->value->name = g_strdup(event_name); |
| 940 | |
| 941 | info->next = ev_list; |
| 942 | ev_list = info; |
| 943 | } |
| 944 | |
| 945 | return ev_list; |
| 946 | } |
| 947 | |
Markus Armbruster | 39a1815 | 2015-09-16 13:06:28 +0200 | [diff] [blame] | 948 | /* |
| 949 | * Minor hack: generated marshalling suppressed for this command |
| 950 | * ('gen': false in the schema) so we can parse the JSON string |
| 951 | * directly into QObject instead of first parsing it with |
| 952 | * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it |
| 953 | * to QObject with generated output marshallers, every time. Instead, |
| 954 | * we do it in test-qmp-input-visitor.c, just to make sure |
| 955 | * qapi-introspect.py's output actually conforms to the schema. |
| 956 | */ |
| 957 | static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data, |
| 958 | Error **errp) |
| 959 | { |
| 960 | *ret_data = qobject_from_json(qmp_schema_json); |
| 961 | } |
| 962 | |
Marc-André Lureau | 5032a16 | 2016-09-12 13:19:02 +0400 | [diff] [blame] | 963 | /* |
Marc-André Lureau | 5032a16 | 2016-09-12 13:19:02 +0400 | [diff] [blame] | 964 | * We used to define commands in qmp-commands.hx in addition to the |
| 965 | * QAPI schema. This permitted defining some of them only in certain |
| 966 | * configurations. query-commands has always reflected that (good, |
| 967 | * because it lets QMP clients figure out what's actually available), |
| 968 | * while query-qmp-schema never did (not so good). This function is a |
| 969 | * hack to keep the configuration-specific commands defined exactly as |
| 970 | * before, even though qmp-commands.hx is gone. |
| 971 | * |
| 972 | * FIXME Educate the QAPI schema on configuration-specific commands, |
| 973 | * and drop this hack. |
| 974 | */ |
| 975 | static void qmp_unregister_commands_hack(void) |
| 976 | { |
| 977 | #ifndef CONFIG_SPICE |
| 978 | qmp_unregister_command("query-spice"); |
| 979 | #endif |
| 980 | #ifndef TARGET_I386 |
| 981 | qmp_unregister_command("rtc-reset-reinjection"); |
| 982 | #endif |
| 983 | #ifndef TARGET_S390X |
| 984 | qmp_unregister_command("dump-skeys"); |
| 985 | #endif |
| 986 | #ifndef TARGET_ARM |
| 987 | qmp_unregister_command("query-gic-capabilities"); |
| 988 | #endif |
Eduardo Habkost | 728b142 | 2016-10-05 16:49:01 -0300 | [diff] [blame] | 989 | #if !defined(TARGET_S390X) |
| 990 | qmp_unregister_command("query-cpu-model-expansion"); |
| 991 | qmp_unregister_command("query-cpu-model-baseline"); |
| 992 | qmp_unregister_command("query-cpu-model-comparison"); |
| 993 | #endif |
| 994 | #if !defined(TARGET_PPC) && !defined(TARGET_ARM) && !defined(TARGET_I386) \ |
| 995 | && !defined(TARGET_S390X) |
| 996 | qmp_unregister_command("query-cpu-definitions"); |
| 997 | #endif |
Marc-André Lureau | 5032a16 | 2016-09-12 13:19:02 +0400 | [diff] [blame] | 998 | } |
| 999 | |
Marc-André Lureau | edcfaef | 2016-09-12 13:19:00 +0400 | [diff] [blame] | 1000 | static void qmp_init_marshal(void) |
| 1001 | { |
| 1002 | qmp_register_command("query-qmp-schema", qmp_query_qmp_schema, |
| 1003 | QCO_NO_OPTIONS); |
| 1004 | qmp_register_command("device_add", qmp_device_add, |
| 1005 | QCO_NO_OPTIONS); |
| 1006 | qmp_register_command("netdev_add", qmp_netdev_add, |
| 1007 | QCO_NO_OPTIONS); |
Marc-André Lureau | 5032a16 | 2016-09-12 13:19:02 +0400 | [diff] [blame] | 1008 | |
| 1009 | /* call it after the rest of qapi_init() */ |
| 1010 | register_module_init(qmp_unregister_commands_hack, MODULE_INIT_QAPI); |
Marc-André Lureau | edcfaef | 2016-09-12 13:19:00 +0400 | [diff] [blame] | 1011 | } |
| 1012 | |
| 1013 | qapi_init(qmp_init_marshal); |
| 1014 | |
Luiz Capitulino | b025c8b | 2011-10-06 14:02:57 -0300 | [diff] [blame] | 1015 | /* set the current CPU defined by the user */ |
| 1016 | int monitor_set_cpu(int cpu_index) |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 1017 | { |
Andreas Färber | 55e5c28 | 2012-12-17 06:18:02 +0100 | [diff] [blame] | 1018 | CPUState *cpu; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 1019 | |
Andreas Färber | 1c8bb3c | 2013-02-15 17:01:09 +0100 | [diff] [blame] | 1020 | cpu = qemu_get_cpu(cpu_index); |
| 1021 | if (cpu == NULL) { |
| 1022 | return -1; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 1023 | } |
Andreas Färber | cb446ec | 2013-05-01 14:24:52 +0200 | [diff] [blame] | 1024 | cur_mon->mon_cpu = cpu; |
Andreas Färber | 1c8bb3c | 2013-02-15 17:01:09 +0100 | [diff] [blame] | 1025 | return 0; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
Pavel Butsykin | caf1531 | 2015-09-22 16:18:17 +0300 | [diff] [blame] | 1028 | CPUState *mon_get_cpu(void) |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 1029 | { |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 1030 | if (!cur_mon->mon_cpu) { |
David Gibson | 27a83f8 | 2016-09-21 15:29:26 +1000 | [diff] [blame] | 1031 | monitor_set_cpu(first_cpu->cpu_index); |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 1032 | } |
Avi Kivity | 4c0960c | 2009-08-17 23:19:53 +0300 | [diff] [blame] | 1033 | cpu_synchronize_state(cur_mon->mon_cpu); |
Peter Crosthwaite | 5bcda5f | 2015-05-24 14:20:40 -0700 | [diff] [blame] | 1034 | return cur_mon->mon_cpu; |
| 1035 | } |
| 1036 | |
Pavel Butsykin | bf95728 | 2015-09-10 18:38:59 +0300 | [diff] [blame] | 1037 | CPUArchState *mon_get_cpu_env(void) |
Peter Crosthwaite | 5bcda5f | 2015-05-24 14:20:40 -0700 | [diff] [blame] | 1038 | { |
| 1039 | return mon_get_cpu()->env_ptr; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
Luiz Capitulino | 99b7796 | 2011-10-24 10:53:44 -0200 | [diff] [blame] | 1042 | int monitor_get_cpu_index(void) |
| 1043 | { |
Peter Crosthwaite | 5bcda5f | 2015-05-24 14:20:40 -0700 | [diff] [blame] | 1044 | return mon_get_cpu()->cpu_index; |
Luiz Capitulino | 99b7796 | 2011-10-24 10:53:44 -0200 | [diff] [blame] | 1045 | } |
| 1046 | |
Markus Armbruster | 1ce6be2 | 2015-02-06 14:18:24 +0100 | [diff] [blame] | 1047 | static void hmp_info_registers(Monitor *mon, const QDict *qdict) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1048 | { |
Peter Crosthwaite | 5bcda5f | 2015-05-24 14:20:40 -0700 | [diff] [blame] | 1049 | cpu_dump_state(mon_get_cpu(), (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
Markus Armbruster | 1ce6be2 | 2015-02-06 14:18:24 +0100 | [diff] [blame] | 1052 | static void hmp_info_jit(Monitor *mon, const QDict *qdict) |
bellard | e3db722 | 2005-01-26 22:00:47 +0000 | [diff] [blame] | 1053 | { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1054 | dump_exec_info((FILE *)mon, monitor_fprintf); |
Sebastian Tanase | 27498be | 2014-07-25 11:56:33 +0200 | [diff] [blame] | 1055 | dump_drift_info((FILE *)mon, monitor_fprintf); |
bellard | e3db722 | 2005-01-26 22:00:47 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
Markus Armbruster | 1ce6be2 | 2015-02-06 14:18:24 +0100 | [diff] [blame] | 1058 | static void hmp_info_opcount(Monitor *mon, const QDict *qdict) |
Max Filippov | 246ae24 | 2014-11-02 11:04:18 +0300 | [diff] [blame] | 1059 | { |
| 1060 | dump_opcount_info((FILE *)mon, monitor_fprintf); |
| 1061 | } |
| 1062 | |
Markus Armbruster | 1ce6be2 | 2015-02-06 14:18:24 +0100 | [diff] [blame] | 1063 | static void hmp_info_history(Monitor *mon, const QDict *qdict) |
bellard | aa45548 | 2004-04-04 13:07:25 +0000 | [diff] [blame] | 1064 | { |
| 1065 | int i; |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 1066 | const char *str; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1067 | |
aliguori | cde76ee | 2009-03-05 23:01:51 +0000 | [diff] [blame] | 1068 | if (!mon->rs) |
| 1069 | return; |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 1070 | i = 0; |
| 1071 | for(;;) { |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 1072 | str = readline_get_history(mon->rs, i); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 1073 | if (!str) |
| 1074 | break; |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1075 | monitor_printf(mon, "%d: '%s'\n", i, str); |
bellard | 8e3a9fd | 2004-10-09 17:32:58 +0000 | [diff] [blame] | 1076 | i++; |
bellard | aa45548 | 2004-04-04 13:07:25 +0000 | [diff] [blame] | 1077 | } |
| 1078 | } |
| 1079 | |
Markus Armbruster | 1ce6be2 | 2015-02-06 14:18:24 +0100 | [diff] [blame] | 1080 | static void hmp_info_cpustats(Monitor *mon, const QDict *qdict) |
j_mayer | 76a6625 | 2007-03-07 08:32:30 +0000 | [diff] [blame] | 1081 | { |
Peter Crosthwaite | 5bcda5f | 2015-05-24 14:20:40 -0700 | [diff] [blame] | 1082 | cpu_dump_statistics(mon_get_cpu(), (FILE *)mon, &monitor_fprintf, 0); |
j_mayer | 76a6625 | 2007-03-07 08:32:30 +0000 | [diff] [blame] | 1083 | } |
j_mayer | 76a6625 | 2007-03-07 08:32:30 +0000 | [diff] [blame] | 1084 | |
Markus Armbruster | 1ce6be2 | 2015-02-06 14:18:24 +0100 | [diff] [blame] | 1085 | static void hmp_info_trace_events(Monitor *mon, const QDict *qdict) |
Prerna Saxena | 22890ab | 2010-06-24 17:04:53 +0530 | [diff] [blame] | 1086 | { |
Lluís Vilanova | bd71211 | 2016-07-11 12:53:51 +0200 | [diff] [blame] | 1087 | const char *name = qdict_get_try_str(qdict, "name"); |
Lluís Vilanova | 77e2b17 | 2016-07-11 12:53:57 +0200 | [diff] [blame] | 1088 | bool has_vcpu = qdict_haskey(qdict, "vcpu"); |
| 1089 | int vcpu = qdict_get_try_int(qdict, "vcpu", 0); |
Lluís Vilanova | bd71211 | 2016-07-11 12:53:51 +0200 | [diff] [blame] | 1090 | TraceEventInfoList *events; |
Lluís Vilanova | 14101d0 | 2014-08-25 13:20:03 +0200 | [diff] [blame] | 1091 | TraceEventInfoList *elem; |
Lluís Vilanova | bd71211 | 2016-07-11 12:53:51 +0200 | [diff] [blame] | 1092 | Error *local_err = NULL; |
| 1093 | |
| 1094 | if (name == NULL) { |
| 1095 | name = "*"; |
| 1096 | } |
Lluís Vilanova | 77e2b17 | 2016-07-11 12:53:57 +0200 | [diff] [blame] | 1097 | if (vcpu < 0) { |
| 1098 | monitor_printf(mon, "argument vcpu must be positive"); |
| 1099 | return; |
| 1100 | } |
Lluís Vilanova | bd71211 | 2016-07-11 12:53:51 +0200 | [diff] [blame] | 1101 | |
Lluís Vilanova | 77e2b17 | 2016-07-11 12:53:57 +0200 | [diff] [blame] | 1102 | 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] | 1103 | if (local_err) { |
| 1104 | error_report_err(local_err); |
| 1105 | return; |
| 1106 | } |
Lluís Vilanova | 14101d0 | 2014-08-25 13:20:03 +0200 | [diff] [blame] | 1107 | |
| 1108 | for (elem = events; elem != NULL; elem = elem->next) { |
| 1109 | monitor_printf(mon, "%s : state %u\n", |
| 1110 | elem->value->name, |
| 1111 | elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0); |
| 1112 | } |
| 1113 | qapi_free_TraceEventInfoList(events); |
Prerna Saxena | 22890ab | 2010-06-24 17:04:53 +0530 | [diff] [blame] | 1114 | } |
Prerna Saxena | 22890ab | 2010-06-24 17:04:53 +0530 | [diff] [blame] | 1115 | |
Markus Armbruster | b8a185b | 2015-03-05 17:29:02 +0100 | [diff] [blame] | 1116 | void qmp_client_migrate_info(const char *protocol, const char *hostname, |
| 1117 | bool has_port, int64_t port, |
| 1118 | bool has_tls_port, int64_t tls_port, |
| 1119 | bool has_cert_subject, const char *cert_subject, |
| 1120 | Error **errp) |
Gerd Hoffmann | e866e23 | 2010-04-23 13:28:21 +0200 | [diff] [blame] | 1121 | { |
Gerd Hoffmann | e866e23 | 2010-04-23 13:28:21 +0200 | [diff] [blame] | 1122 | if (strcmp(protocol, "spice") == 0) { |
Markus Armbruster | b8a185b | 2015-03-05 17:29:02 +0100 | [diff] [blame] | 1123 | if (!qemu_using_spice(errp)) { |
| 1124 | return; |
Gerd Hoffmann | e866e23 | 2010-04-23 13:28:21 +0200 | [diff] [blame] | 1125 | } |
| 1126 | |
Markus Armbruster | b8a185b | 2015-03-05 17:29:02 +0100 | [diff] [blame] | 1127 | if (!has_port && !has_tls_port) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 1128 | error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port"); |
Markus Armbruster | b8a185b | 2015-03-05 17:29:02 +0100 | [diff] [blame] | 1129 | return; |
Yonit Halperin | 6ec5dae | 2012-03-18 09:42:39 +0200 | [diff] [blame] | 1130 | } |
| 1131 | |
Markus Armbruster | b8a185b | 2015-03-05 17:29:02 +0100 | [diff] [blame] | 1132 | if (qemu_spice_migrate_info(hostname, |
| 1133 | has_port ? port : -1, |
| 1134 | has_tls_port ? tls_port : -1, |
| 1135 | cert_subject)) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 1136 | error_setg(errp, QERR_UNDEFINED_ERROR); |
Markus Armbruster | b8a185b | 2015-03-05 17:29:02 +0100 | [diff] [blame] | 1137 | return; |
Gerd Hoffmann | e866e23 | 2010-04-23 13:28:21 +0200 | [diff] [blame] | 1138 | } |
Markus Armbruster | b8a185b | 2015-03-05 17:29:02 +0100 | [diff] [blame] | 1139 | return; |
Gerd Hoffmann | e866e23 | 2010-04-23 13:28:21 +0200 | [diff] [blame] | 1140 | } |
| 1141 | |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 1142 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice"); |
Gerd Hoffmann | e866e23 | 2010-04-23 13:28:21 +0200 | [diff] [blame] | 1143 | } |
| 1144 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 1145 | static void hmp_logfile(Monitor *mon, const QDict *qdict) |
pbrook | e735b91 | 2007-06-30 13:53:24 +0000 | [diff] [blame] | 1146 | { |
Markus Armbruster | daa76aa | 2016-06-15 19:27:16 +0200 | [diff] [blame] | 1147 | Error *err = NULL; |
| 1148 | |
| 1149 | qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err); |
| 1150 | if (err) { |
| 1151 | error_report_err(err); |
| 1152 | } |
pbrook | e735b91 | 2007-06-30 13:53:24 +0000 | [diff] [blame] | 1153 | } |
| 1154 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 1155 | static void hmp_log(Monitor *mon, const QDict *qdict) |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1156 | { |
| 1157 | int mask; |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 1158 | const char *items = qdict_get_str(qdict, "items"); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1159 | |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1160 | if (!strcmp(items, "none")) { |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1161 | mask = 0; |
| 1162 | } else { |
Peter Maydell | 4fde1eb | 2013-02-11 16:41:22 +0000 | [diff] [blame] | 1163 | mask = qemu_str_to_log_mask(items); |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1164 | if (!mask) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1165 | help_cmd(mon, "log"); |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1166 | return; |
| 1167 | } |
| 1168 | } |
Peter Maydell | 24537a0 | 2013-02-11 16:41:23 +0000 | [diff] [blame] | 1169 | qemu_set_log(mask); |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 1172 | static void hmp_singlestep(Monitor *mon, const QDict *qdict) |
aurel32 | 1b530a6 | 2009-04-05 20:08:59 +0000 | [diff] [blame] | 1173 | { |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 1174 | const char *option = qdict_get_try_str(qdict, "option"); |
aurel32 | 1b530a6 | 2009-04-05 20:08:59 +0000 | [diff] [blame] | 1175 | if (!option || !strcmp(option, "on")) { |
| 1176 | singlestep = 1; |
| 1177 | } else if (!strcmp(option, "off")) { |
| 1178 | singlestep = 0; |
| 1179 | } else { |
| 1180 | monitor_printf(mon, "unexpected option %s\n", option); |
| 1181 | } |
| 1182 | } |
| 1183 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 1184 | static void hmp_gdbserver(Monitor *mon, const QDict *qdict) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1185 | { |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 1186 | const char *device = qdict_get_try_str(qdict, "device"); |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 1187 | if (!device) |
| 1188 | device = "tcp::" DEFAULT_GDBSTUB_PORT; |
| 1189 | if (gdbserver_start(device) < 0) { |
| 1190 | monitor_printf(mon, "Could not open gdbserver on device '%s'\n", |
| 1191 | device); |
| 1192 | } else if (strcmp(device, "none") == 0) { |
aliguori | 36556b2 | 2009-03-28 18:05:53 +0000 | [diff] [blame] | 1193 | monitor_printf(mon, "Disabled gdbserver\n"); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1194 | } else { |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 1195 | monitor_printf(mon, "Waiting for gdb connection on device '%s'\n", |
| 1196 | device); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1197 | } |
| 1198 | } |
| 1199 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 1200 | static void hmp_watchdog_action(Monitor *mon, const QDict *qdict) |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 1201 | { |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 1202 | const char *action = qdict_get_str(qdict, "action"); |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 1203 | if (select_watchdog_action(action) == -1) { |
| 1204 | monitor_printf(mon, "Unknown watchdog action '%s'\n", action); |
| 1205 | } |
| 1206 | } |
| 1207 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1208 | static void monitor_printc(Monitor *mon, int c) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1209 | { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1210 | monitor_printf(mon, "'"); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1211 | switch(c) { |
| 1212 | case '\'': |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1213 | monitor_printf(mon, "\\'"); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1214 | break; |
| 1215 | case '\\': |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1216 | monitor_printf(mon, "\\\\"); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1217 | break; |
| 1218 | case '\n': |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1219 | monitor_printf(mon, "\\n"); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1220 | break; |
| 1221 | case '\r': |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1222 | monitor_printf(mon, "\\r"); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1223 | break; |
| 1224 | default: |
| 1225 | if (c >= 32 && c <= 126) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1226 | monitor_printf(mon, "%c", c); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1227 | } else { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1228 | monitor_printf(mon, "\\x%02x", c); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1229 | } |
| 1230 | break; |
| 1231 | } |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1232 | monitor_printf(mon, "'"); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1233 | } |
| 1234 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1235 | static void memory_dump(Monitor *mon, int count, int format, int wsize, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1236 | hwaddr addr, int is_physical) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1237 | { |
Blue Swirl | 23842aa | 2010-01-12 20:27:43 +0000 | [diff] [blame] | 1238 | int l, line_size, i, max_digits, len; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1239 | uint8_t buf[16]; |
| 1240 | uint64_t v; |
| 1241 | |
| 1242 | if (format == 'i') { |
Peter Crosthwaite | 5bcda5f | 2015-05-24 14:20:40 -0700 | [diff] [blame] | 1243 | int flags = 0; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1244 | #ifdef TARGET_I386 |
Peter Crosthwaite | 5bcda5f | 2015-05-24 14:20:40 -0700 | [diff] [blame] | 1245 | CPUArchState *env = mon_get_cpu_env(); |
bellard | 4c27ba2 | 2004-04-25 18:05:08 +0000 | [diff] [blame] | 1246 | if (wsize == 2) { |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1247 | flags = 1; |
bellard | 4c27ba2 | 2004-04-25 18:05:08 +0000 | [diff] [blame] | 1248 | } else if (wsize == 4) { |
| 1249 | flags = 0; |
| 1250 | } else { |
bellard | 6a15fd1 | 2006-04-12 21:07:07 +0000 | [diff] [blame] | 1251 | /* as default we use the current CS size */ |
bellard | 4c27ba2 | 2004-04-25 18:05:08 +0000 | [diff] [blame] | 1252 | flags = 0; |
bellard | 6a15fd1 | 2006-04-12 21:07:07 +0000 | [diff] [blame] | 1253 | if (env) { |
| 1254 | #ifdef TARGET_X86_64 |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1255 | if ((env->efer & MSR_EFER_LMA) && |
bellard | 6a15fd1 | 2006-04-12 21:07:07 +0000 | [diff] [blame] | 1256 | (env->segs[R_CS].flags & DESC_L_MASK)) |
| 1257 | flags = 2; |
| 1258 | else |
| 1259 | #endif |
| 1260 | if (!(env->segs[R_CS].flags & DESC_B_MASK)) |
| 1261 | flags = 1; |
| 1262 | } |
bellard | 4c27ba2 | 2004-04-25 18:05:08 +0000 | [diff] [blame] | 1263 | } |
| 1264 | #endif |
Tom Musta | 1c38f84 | 2014-04-09 14:53:24 -0500 | [diff] [blame] | 1265 | #ifdef TARGET_PPC |
Peter Crosthwaite | 5bcda5f | 2015-05-24 14:20:40 -0700 | [diff] [blame] | 1266 | CPUArchState *env = mon_get_cpu_env(); |
Tom Musta | 1c38f84 | 2014-04-09 14:53:24 -0500 | [diff] [blame] | 1267 | flags = msr_le << 16; |
| 1268 | flags |= env->bfd_mach; |
| 1269 | #endif |
Peter Crosthwaite | d49190c | 2015-05-24 14:20:41 -0700 | [diff] [blame] | 1270 | monitor_disas(mon, mon_get_cpu(), addr, count, is_physical, flags); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1271 | return; |
| 1272 | } |
| 1273 | |
| 1274 | len = wsize * count; |
| 1275 | if (wsize == 1) |
| 1276 | line_size = 8; |
| 1277 | else |
| 1278 | line_size = 16; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1279 | max_digits = 0; |
| 1280 | |
| 1281 | switch(format) { |
| 1282 | case 'o': |
| 1283 | max_digits = (wsize * 8 + 2) / 3; |
| 1284 | break; |
| 1285 | default: |
| 1286 | case 'x': |
| 1287 | max_digits = (wsize * 8) / 4; |
| 1288 | break; |
| 1289 | case 'u': |
| 1290 | case 'd': |
| 1291 | max_digits = (wsize * 8 * 10 + 32) / 33; |
| 1292 | break; |
| 1293 | case 'c': |
| 1294 | wsize = 1; |
| 1295 | break; |
| 1296 | } |
| 1297 | |
| 1298 | while (len > 0) { |
blueswir1 | 7743e58 | 2007-09-24 18:39:04 +0000 | [diff] [blame] | 1299 | if (is_physical) |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1300 | monitor_printf(mon, TARGET_FMT_plx ":", addr); |
blueswir1 | 7743e58 | 2007-09-24 18:39:04 +0000 | [diff] [blame] | 1301 | else |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1302 | monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1303 | l = len; |
| 1304 | if (l > line_size) |
| 1305 | l = line_size; |
| 1306 | if (is_physical) { |
Stefan Weil | 54f7b4a | 2011-04-10 18:23:39 +0200 | [diff] [blame] | 1307 | cpu_physical_memory_read(addr, buf, l); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1308 | } else { |
Peter Crosthwaite | 5bcda5f | 2015-05-24 14:20:40 -0700 | [diff] [blame] | 1309 | if (cpu_memory_rw_debug(mon_get_cpu(), addr, buf, l, 0) < 0) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1310 | monitor_printf(mon, " Cannot access memory\n"); |
aliguori | c8f79b6 | 2008-08-18 14:00:20 +0000 | [diff] [blame] | 1311 | break; |
| 1312 | } |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1313 | } |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1314 | i = 0; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1315 | while (i < l) { |
| 1316 | switch(wsize) { |
| 1317 | default: |
| 1318 | case 1: |
Peter Maydell | 24e6030 | 2015-01-20 15:19:32 +0000 | [diff] [blame] | 1319 | v = ldub_p(buf + i); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1320 | break; |
| 1321 | case 2: |
Peter Maydell | 24e6030 | 2015-01-20 15:19:32 +0000 | [diff] [blame] | 1322 | v = lduw_p(buf + i); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1323 | break; |
| 1324 | case 4: |
Peter Maydell | 24e6030 | 2015-01-20 15:19:32 +0000 | [diff] [blame] | 1325 | v = (uint32_t)ldl_p(buf + i); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1326 | break; |
| 1327 | case 8: |
Peter Maydell | 24e6030 | 2015-01-20 15:19:32 +0000 | [diff] [blame] | 1328 | v = ldq_p(buf + i); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1329 | break; |
| 1330 | } |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1331 | monitor_printf(mon, " "); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1332 | switch(format) { |
| 1333 | case 'o': |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1334 | monitor_printf(mon, "%#*" PRIo64, max_digits, v); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1335 | break; |
| 1336 | case 'x': |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1337 | monitor_printf(mon, "0x%0*" PRIx64, max_digits, v); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1338 | break; |
| 1339 | case 'u': |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1340 | monitor_printf(mon, "%*" PRIu64, max_digits, v); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1341 | break; |
| 1342 | case 'd': |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1343 | monitor_printf(mon, "%*" PRId64, max_digits, v); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1344 | break; |
| 1345 | case 'c': |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1346 | monitor_printc(mon, v); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1347 | break; |
| 1348 | } |
| 1349 | i += wsize; |
| 1350 | } |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1351 | monitor_printf(mon, "\n"); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1352 | addr += l; |
| 1353 | len -= l; |
| 1354 | } |
| 1355 | } |
| 1356 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 1357 | static void hmp_memory_dump(Monitor *mon, const QDict *qdict) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1358 | { |
Luiz Capitulino | 1bd1442 | 2009-08-28 15:27:17 -0300 | [diff] [blame] | 1359 | int count = qdict_get_int(qdict, "count"); |
| 1360 | int format = qdict_get_int(qdict, "format"); |
| 1361 | int size = qdict_get_int(qdict, "size"); |
| 1362 | target_long addr = qdict_get_int(qdict, "addr"); |
| 1363 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1364 | memory_dump(mon, count, format, size, addr, 0); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1365 | } |
| 1366 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 1367 | static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1368 | { |
Luiz Capitulino | 1bd1442 | 2009-08-28 15:27:17 -0300 | [diff] [blame] | 1369 | int count = qdict_get_int(qdict, "count"); |
| 1370 | int format = qdict_get_int(qdict, "format"); |
| 1371 | int size = qdict_get_int(qdict, "size"); |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1372 | hwaddr addr = qdict_get_int(qdict, "addr"); |
Luiz Capitulino | 1bd1442 | 2009-08-28 15:27:17 -0300 | [diff] [blame] | 1373 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1374 | memory_dump(mon, count, format, size, addr, 1); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
Luiz Capitulino | 1bd1442 | 2009-08-28 15:27:17 -0300 | [diff] [blame] | 1377 | static void do_print(Monitor *mon, const QDict *qdict) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1378 | { |
Luiz Capitulino | 1bd1442 | 2009-08-28 15:27:17 -0300 | [diff] [blame] | 1379 | int format = qdict_get_int(qdict, "format"); |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1380 | hwaddr val = qdict_get_int(qdict, "val"); |
Luiz Capitulino | 1bd1442 | 2009-08-28 15:27:17 -0300 | [diff] [blame] | 1381 | |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1382 | switch(format) { |
| 1383 | case 'o': |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1384 | monitor_printf(mon, "%#" HWADDR_PRIo, val); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1385 | break; |
| 1386 | case 'x': |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1387 | monitor_printf(mon, "%#" HWADDR_PRIx, val); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1388 | break; |
| 1389 | case 'u': |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1390 | monitor_printf(mon, "%" HWADDR_PRIu, val); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1391 | break; |
| 1392 | default: |
| 1393 | case 'd': |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 1394 | monitor_printf(mon, "%" HWADDR_PRId, val); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1395 | break; |
| 1396 | case 'c': |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1397 | monitor_printc(mon, val); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1398 | break; |
| 1399 | } |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1400 | monitor_printf(mon, "\n"); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 1403 | static void hmp_sum(Monitor *mon, const QDict *qdict) |
bellard | e4cf1ad | 2005-06-04 20:15:57 +0000 | [diff] [blame] | 1404 | { |
| 1405 | uint32_t addr; |
bellard | e4cf1ad | 2005-06-04 20:15:57 +0000 | [diff] [blame] | 1406 | uint16_t sum; |
Luiz Capitulino | f18c16d | 2009-08-28 15:27:14 -0300 | [diff] [blame] | 1407 | uint32_t start = qdict_get_int(qdict, "start"); |
| 1408 | uint32_t size = qdict_get_int(qdict, "size"); |
bellard | e4cf1ad | 2005-06-04 20:15:57 +0000 | [diff] [blame] | 1409 | |
| 1410 | sum = 0; |
| 1411 | for(addr = start; addr < (start + size); addr++) { |
Peter Maydell | 42874d3 | 2015-04-26 16:49:24 +0100 | [diff] [blame] | 1412 | uint8_t val = address_space_ldub(&address_space_memory, addr, |
| 1413 | MEMTXATTRS_UNSPECIFIED, NULL); |
bellard | e4cf1ad | 2005-06-04 20:15:57 +0000 | [diff] [blame] | 1414 | /* BSD sum algorithm ('sum' Unix command) */ |
| 1415 | sum = (sum >> 1) | (sum << 15); |
Stefan Weil | 54f7b4a | 2011-04-10 18:23:39 +0200 | [diff] [blame] | 1416 | sum += val; |
bellard | e4cf1ad | 2005-06-04 20:15:57 +0000 | [diff] [blame] | 1417 | } |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1418 | monitor_printf(mon, "%05d\n", sum); |
bellard | e4cf1ad | 2005-06-04 20:15:57 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
bellard | 13224a8 | 2006-07-14 22:03:35 +0000 | [diff] [blame] | 1421 | static int mouse_button_state; |
| 1422 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 1423 | static void hmp_mouse_move(Monitor *mon, const QDict *qdict) |
bellard | 13224a8 | 2006-07-14 22:03:35 +0000 | [diff] [blame] | 1424 | { |
Gerd Hoffmann | c751a74 | 2013-12-04 15:02:28 +0100 | [diff] [blame] | 1425 | int dx, dy, dz, button; |
Luiz Capitulino | 1d4daa9 | 2009-08-28 15:27:15 -0300 | [diff] [blame] | 1426 | const char *dx_str = qdict_get_str(qdict, "dx_str"); |
| 1427 | const char *dy_str = qdict_get_str(qdict, "dy_str"); |
| 1428 | const char *dz_str = qdict_get_try_str(qdict, "dz_str"); |
Gerd Hoffmann | c751a74 | 2013-12-04 15:02:28 +0100 | [diff] [blame] | 1429 | |
bellard | 13224a8 | 2006-07-14 22:03:35 +0000 | [diff] [blame] | 1430 | dx = strtol(dx_str, NULL, 0); |
| 1431 | dy = strtol(dy_str, NULL, 0); |
Gerd Hoffmann | c751a74 | 2013-12-04 15:02:28 +0100 | [diff] [blame] | 1432 | qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx); |
| 1433 | qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy); |
| 1434 | |
| 1435 | if (dz_str) { |
bellard | 13224a8 | 2006-07-14 22:03:35 +0000 | [diff] [blame] | 1436 | dz = strtol(dz_str, NULL, 0); |
Gerd Hoffmann | c751a74 | 2013-12-04 15:02:28 +0100 | [diff] [blame] | 1437 | if (dz != 0) { |
Gerd Hoffmann | f22d0af | 2016-01-12 12:14:12 +0100 | [diff] [blame] | 1438 | button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN; |
Gerd Hoffmann | c751a74 | 2013-12-04 15:02:28 +0100 | [diff] [blame] | 1439 | qemu_input_queue_btn(NULL, button, true); |
| 1440 | qemu_input_event_sync(); |
| 1441 | qemu_input_queue_btn(NULL, button, false); |
| 1442 | } |
| 1443 | } |
| 1444 | qemu_input_event_sync(); |
bellard | 13224a8 | 2006-07-14 22:03:35 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 1447 | static void hmp_mouse_button(Monitor *mon, const QDict *qdict) |
bellard | 13224a8 | 2006-07-14 22:03:35 +0000 | [diff] [blame] | 1448 | { |
Eric Blake | 7fb1cf1 | 2015-11-18 01:52:57 -0700 | [diff] [blame] | 1449 | static uint32_t bmap[INPUT_BUTTON__MAX] = { |
Gerd Hoffmann | c751a74 | 2013-12-04 15:02:28 +0100 | [diff] [blame] | 1450 | [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON, |
| 1451 | [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON, |
| 1452 | [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON, |
| 1453 | }; |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 1454 | int button_state = qdict_get_int(qdict, "button_state"); |
Gerd Hoffmann | c751a74 | 2013-12-04 15:02:28 +0100 | [diff] [blame] | 1455 | |
| 1456 | if (mouse_button_state == button_state) { |
| 1457 | return; |
| 1458 | } |
| 1459 | qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state); |
| 1460 | qemu_input_event_sync(); |
bellard | 13224a8 | 2006-07-14 22:03:35 +0000 | [diff] [blame] | 1461 | mouse_button_state = button_state; |
bellard | 13224a8 | 2006-07-14 22:03:35 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 1464 | static void hmp_ioport_read(Monitor *mon, const QDict *qdict) |
bellard | 3440557 | 2004-06-08 00:55:58 +0000 | [diff] [blame] | 1465 | { |
Luiz Capitulino | aa93e39 | 2009-08-28 15:27:18 -0300 | [diff] [blame] | 1466 | int size = qdict_get_int(qdict, "size"); |
| 1467 | int addr = qdict_get_int(qdict, "addr"); |
| 1468 | int has_index = qdict_haskey(qdict, "index"); |
bellard | 3440557 | 2004-06-08 00:55:58 +0000 | [diff] [blame] | 1469 | uint32_t val; |
| 1470 | int suffix; |
| 1471 | |
| 1472 | if (has_index) { |
Luiz Capitulino | aa93e39 | 2009-08-28 15:27:18 -0300 | [diff] [blame] | 1473 | int index = qdict_get_int(qdict, "index"); |
Blue Swirl | afcea8c | 2009-09-20 16:05:47 +0000 | [diff] [blame] | 1474 | cpu_outb(addr & IOPORTS_MASK, index & 0xff); |
bellard | 3440557 | 2004-06-08 00:55:58 +0000 | [diff] [blame] | 1475 | addr++; |
| 1476 | } |
| 1477 | addr &= 0xffff; |
| 1478 | |
| 1479 | switch(size) { |
| 1480 | default: |
| 1481 | case 1: |
Blue Swirl | afcea8c | 2009-09-20 16:05:47 +0000 | [diff] [blame] | 1482 | val = cpu_inb(addr); |
bellard | 3440557 | 2004-06-08 00:55:58 +0000 | [diff] [blame] | 1483 | suffix = 'b'; |
| 1484 | break; |
| 1485 | case 2: |
Blue Swirl | afcea8c | 2009-09-20 16:05:47 +0000 | [diff] [blame] | 1486 | val = cpu_inw(addr); |
bellard | 3440557 | 2004-06-08 00:55:58 +0000 | [diff] [blame] | 1487 | suffix = 'w'; |
| 1488 | break; |
| 1489 | case 4: |
Blue Swirl | afcea8c | 2009-09-20 16:05:47 +0000 | [diff] [blame] | 1490 | val = cpu_inl(addr); |
bellard | 3440557 | 2004-06-08 00:55:58 +0000 | [diff] [blame] | 1491 | suffix = 'l'; |
| 1492 | break; |
| 1493 | } |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1494 | monitor_printf(mon, "port%c[0x%04x] = %#0*x\n", |
| 1495 | suffix, addr, size * 2, val); |
bellard | 3440557 | 2004-06-08 00:55:58 +0000 | [diff] [blame] | 1496 | } |
bellard | a3a91a3 | 2004-06-04 11:06:21 +0000 | [diff] [blame] | 1497 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 1498 | static void hmp_ioport_write(Monitor *mon, const QDict *qdict) |
Jan Kiszka | f114784 | 2009-07-14 10:20:11 +0200 | [diff] [blame] | 1499 | { |
Luiz Capitulino | 1bd1442 | 2009-08-28 15:27:17 -0300 | [diff] [blame] | 1500 | int size = qdict_get_int(qdict, "size"); |
| 1501 | int addr = qdict_get_int(qdict, "addr"); |
| 1502 | int val = qdict_get_int(qdict, "val"); |
| 1503 | |
Jan Kiszka | f114784 | 2009-07-14 10:20:11 +0200 | [diff] [blame] | 1504 | addr &= IOPORTS_MASK; |
| 1505 | |
| 1506 | switch (size) { |
| 1507 | default: |
| 1508 | case 1: |
Blue Swirl | afcea8c | 2009-09-20 16:05:47 +0000 | [diff] [blame] | 1509 | cpu_outb(addr, val); |
Jan Kiszka | f114784 | 2009-07-14 10:20:11 +0200 | [diff] [blame] | 1510 | break; |
| 1511 | case 2: |
Blue Swirl | afcea8c | 2009-09-20 16:05:47 +0000 | [diff] [blame] | 1512 | cpu_outw(addr, val); |
Jan Kiszka | f114784 | 2009-07-14 10:20:11 +0200 | [diff] [blame] | 1513 | break; |
| 1514 | case 4: |
Blue Swirl | afcea8c | 2009-09-20 16:05:47 +0000 | [diff] [blame] | 1515 | cpu_outl(addr, val); |
Jan Kiszka | f114784 | 2009-07-14 10:20:11 +0200 | [diff] [blame] | 1516 | break; |
| 1517 | } |
| 1518 | } |
| 1519 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 1520 | static void hmp_boot_set(Monitor *mon, const QDict *qdict) |
aurel32 | 0ecdffb | 2008-05-04 20:11:34 +0000 | [diff] [blame] | 1521 | { |
Gonglei | f183993 | 2014-12-03 18:20:58 +0000 | [diff] [blame] | 1522 | Error *local_err = NULL; |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 1523 | const char *bootdevice = qdict_get_str(qdict, "bootdevice"); |
aurel32 | 0ecdffb | 2008-05-04 20:11:34 +0000 | [diff] [blame] | 1524 | |
Gonglei | f183993 | 2014-12-03 18:20:58 +0000 | [diff] [blame] | 1525 | qemu_boot_set(bootdevice, &local_err); |
| 1526 | if (local_err) { |
Markus Armbruster | 193227f | 2015-12-18 16:35:06 +0100 | [diff] [blame] | 1527 | error_report_err(local_err); |
aurel32 | 0ecdffb | 2008-05-04 20:11:34 +0000 | [diff] [blame] | 1528 | } else { |
Gonglei | f183993 | 2014-12-03 18:20:58 +0000 | [diff] [blame] | 1529 | monitor_printf(mon, "boot device list now set to %s\n", bootdevice); |
aurel32 | 0ecdffb | 2008-05-04 20:11:34 +0000 | [diff] [blame] | 1530 | } |
| 1531 | } |
| 1532 | |
Markus Armbruster | 1ce6be2 | 2015-02-06 14:18:24 +0100 | [diff] [blame] | 1533 | static void hmp_info_mtree(Monitor *mon, const QDict *qdict) |
Blue Swirl | 314e298 | 2011-09-11 20:22:05 +0000 | [diff] [blame] | 1534 | { |
| 1535 | mtree_info((fprintf_function)monitor_printf, mon); |
| 1536 | } |
| 1537 | |
Markus Armbruster | 1ce6be2 | 2015-02-06 14:18:24 +0100 | [diff] [blame] | 1538 | static void hmp_info_numa(Monitor *mon, const QDict *qdict) |
aliguori | 030ea37 | 2009-04-21 22:30:47 +0000 | [diff] [blame] | 1539 | { |
aliguori | b28b623 | 2009-04-22 20:20:29 +0000 | [diff] [blame] | 1540 | int i; |
Andreas Färber | 1b1ed8d | 2012-12-17 04:22:03 +0100 | [diff] [blame] | 1541 | CPUState *cpu; |
zhanghailiang | 5b009e4 | 2014-11-04 19:49:30 +0800 | [diff] [blame] | 1542 | uint64_t *node_mem; |
aliguori | 030ea37 | 2009-04-21 22:30:47 +0000 | [diff] [blame] | 1543 | |
zhanghailiang | 5b009e4 | 2014-11-04 19:49:30 +0800 | [diff] [blame] | 1544 | node_mem = g_new0(uint64_t, nb_numa_nodes); |
| 1545 | query_numa_node_mem(node_mem); |
aliguori | 030ea37 | 2009-04-21 22:30:47 +0000 | [diff] [blame] | 1546 | monitor_printf(mon, "%d nodes\n", nb_numa_nodes); |
| 1547 | for (i = 0; i < nb_numa_nodes; i++) { |
| 1548 | monitor_printf(mon, "node %d cpus:", i); |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 1549 | CPU_FOREACH(cpu) { |
Andreas Färber | 1b1ed8d | 2012-12-17 04:22:03 +0100 | [diff] [blame] | 1550 | if (cpu->numa_node == i) { |
Andreas Färber | 55e5c28 | 2012-12-17 06:18:02 +0100 | [diff] [blame] | 1551 | monitor_printf(mon, " %d", cpu->cpu_index); |
aliguori | 030ea37 | 2009-04-21 22:30:47 +0000 | [diff] [blame] | 1552 | } |
| 1553 | } |
| 1554 | monitor_printf(mon, "\n"); |
| 1555 | monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i, |
zhanghailiang | 5b009e4 | 2014-11-04 19:49:30 +0800 | [diff] [blame] | 1556 | node_mem[i] >> 20); |
aliguori | 030ea37 | 2009-04-21 22:30:47 +0000 | [diff] [blame] | 1557 | } |
zhanghailiang | 5b009e4 | 2014-11-04 19:49:30 +0800 | [diff] [blame] | 1558 | g_free(node_mem); |
aliguori | 030ea37 | 2009-04-21 22:30:47 +0000 | [diff] [blame] | 1559 | } |
| 1560 | |
bellard | 5f1ce94 | 2006-02-08 22:40:15 +0000 | [diff] [blame] | 1561 | #ifdef CONFIG_PROFILER |
| 1562 | |
Alexey Kardashevskiy | 89d5cbd | 2015-03-16 14:57:38 +1100 | [diff] [blame] | 1563 | int64_t tcg_time; |
Aurelien Jarno | e9a6625 | 2009-09-30 14:09:52 +0200 | [diff] [blame] | 1564 | int64_t dev_time; |
| 1565 | |
Markus Armbruster | 1ce6be2 | 2015-02-06 14:18:24 +0100 | [diff] [blame] | 1566 | static void hmp_info_profile(Monitor *mon, const QDict *qdict) |
bellard | 5f1ce94 | 2006-02-08 22:40:15 +0000 | [diff] [blame] | 1567 | { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1568 | monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n", |
Rutuja Shah | 73bcb24 | 2016-03-21 21:32:30 +0530 | [diff] [blame] | 1569 | dev_time, dev_time / (double)NANOSECONDS_PER_SECOND); |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1570 | monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n", |
Rutuja Shah | 73bcb24 | 2016-03-21 21:32:30 +0530 | [diff] [blame] | 1571 | tcg_time, tcg_time / (double)NANOSECONDS_PER_SECOND); |
Alexey Kardashevskiy | 89d5cbd | 2015-03-16 14:57:38 +1100 | [diff] [blame] | 1572 | tcg_time = 0; |
bellard | 5f1ce94 | 2006-02-08 22:40:15 +0000 | [diff] [blame] | 1573 | dev_time = 0; |
bellard | 5f1ce94 | 2006-02-08 22:40:15 +0000 | [diff] [blame] | 1574 | } |
| 1575 | #else |
Markus Armbruster | 1ce6be2 | 2015-02-06 14:18:24 +0100 | [diff] [blame] | 1576 | static void hmp_info_profile(Monitor *mon, const QDict *qdict) |
bellard | 5f1ce94 | 2006-02-08 22:40:15 +0000 | [diff] [blame] | 1577 | { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 1578 | monitor_printf(mon, "Internal profiler not compiled\n"); |
bellard | 5f1ce94 | 2006-02-08 22:40:15 +0000 | [diff] [blame] | 1579 | } |
| 1580 | #endif |
| 1581 | |
bellard | ec36b69 | 2006-07-16 18:57:03 +0000 | [diff] [blame] | 1582 | /* Capture support */ |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1583 | static QLIST_HEAD (capture_list_head, CaptureState) capture_head; |
bellard | ec36b69 | 2006-07-16 18:57:03 +0000 | [diff] [blame] | 1584 | |
Markus Armbruster | 1ce6be2 | 2015-02-06 14:18:24 +0100 | [diff] [blame] | 1585 | static void hmp_info_capture(Monitor *mon, const QDict *qdict) |
bellard | ec36b69 | 2006-07-16 18:57:03 +0000 | [diff] [blame] | 1586 | { |
| 1587 | int i; |
| 1588 | CaptureState *s; |
| 1589 | |
| 1590 | 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] | 1591 | monitor_printf(mon, "[%d]: ", i); |
bellard | ec36b69 | 2006-07-16 18:57:03 +0000 | [diff] [blame] | 1592 | s->ops.info (s->opaque); |
| 1593 | } |
| 1594 | } |
| 1595 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 1596 | static void hmp_stopcapture(Monitor *mon, const QDict *qdict) |
bellard | ec36b69 | 2006-07-16 18:57:03 +0000 | [diff] [blame] | 1597 | { |
| 1598 | int i; |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 1599 | int n = qdict_get_int(qdict, "n"); |
bellard | ec36b69 | 2006-07-16 18:57:03 +0000 | [diff] [blame] | 1600 | CaptureState *s; |
| 1601 | |
| 1602 | for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) { |
| 1603 | if (i == n) { |
| 1604 | s->ops.destroy (s->opaque); |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1605 | QLIST_REMOVE (s, entries); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1606 | g_free (s); |
bellard | ec36b69 | 2006-07-16 18:57:03 +0000 | [diff] [blame] | 1607 | return; |
| 1608 | } |
| 1609 | } |
| 1610 | } |
| 1611 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 1612 | static void hmp_wavcapture(Monitor *mon, const QDict *qdict) |
bellard | ec36b69 | 2006-07-16 18:57:03 +0000 | [diff] [blame] | 1613 | { |
Luiz Capitulino | c192548 | 2009-08-28 15:27:19 -0300 | [diff] [blame] | 1614 | const char *path = qdict_get_str(qdict, "path"); |
| 1615 | int has_freq = qdict_haskey(qdict, "freq"); |
| 1616 | int freq = qdict_get_try_int(qdict, "freq", -1); |
| 1617 | int has_bits = qdict_haskey(qdict, "bits"); |
| 1618 | int bits = qdict_get_try_int(qdict, "bits", -1); |
| 1619 | int has_channels = qdict_haskey(qdict, "nchannels"); |
| 1620 | int nchannels = qdict_get_try_int(qdict, "nchannels", -1); |
bellard | ec36b69 | 2006-07-16 18:57:03 +0000 | [diff] [blame] | 1621 | CaptureState *s; |
| 1622 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1623 | s = g_malloc0 (sizeof (*s)); |
bellard | ec36b69 | 2006-07-16 18:57:03 +0000 | [diff] [blame] | 1624 | |
| 1625 | freq = has_freq ? freq : 44100; |
| 1626 | bits = has_bits ? bits : 16; |
| 1627 | nchannels = has_channels ? nchannels : 2; |
| 1628 | |
| 1629 | if (wav_start_capture (s, path, freq, bits, nchannels)) { |
Isaku Yamahata | d00b261 | 2011-01-21 19:53:55 +0900 | [diff] [blame] | 1630 | monitor_printf(mon, "Failed to add wave capture\n"); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1631 | g_free (s); |
Isaku Yamahata | d00b261 | 2011-01-21 19:53:55 +0900 | [diff] [blame] | 1632 | return; |
bellard | ec36b69 | 2006-07-16 18:57:03 +0000 | [diff] [blame] | 1633 | } |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1634 | QLIST_INSERT_HEAD (&capture_head, s, entries); |
bellard | ec36b69 | 2006-07-16 18:57:03 +0000 | [diff] [blame] | 1635 | } |
bellard | ec36b69 | 2006-07-16 18:57:03 +0000 | [diff] [blame] | 1636 | |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 1637 | static qemu_acl *find_acl(Monitor *mon, const char *name) |
aliguori | 76655d6 | 2009-03-06 20:27:37 +0000 | [diff] [blame] | 1638 | { |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 1639 | qemu_acl *acl = qemu_acl_find(name); |
aliguori | 76655d6 | 2009-03-06 20:27:37 +0000 | [diff] [blame] | 1640 | |
aliguori | 76655d6 | 2009-03-06 20:27:37 +0000 | [diff] [blame] | 1641 | if (!acl) { |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 1642 | monitor_printf(mon, "acl: unknown list '%s'\n", name); |
aliguori | 76655d6 | 2009-03-06 20:27:37 +0000 | [diff] [blame] | 1643 | } |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 1644 | return acl; |
| 1645 | } |
aliguori | 76655d6 | 2009-03-06 20:27:37 +0000 | [diff] [blame] | 1646 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 1647 | static void hmp_acl_show(Monitor *mon, const QDict *qdict) |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 1648 | { |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 1649 | const char *aclname = qdict_get_str(qdict, "aclname"); |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 1650 | qemu_acl *acl = find_acl(mon, aclname); |
| 1651 | qemu_acl_entry *entry; |
| 1652 | int i = 0; |
| 1653 | |
| 1654 | if (acl) { |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 1655 | monitor_printf(mon, "policy: %s\n", |
aliguori | 76655d6 | 2009-03-06 20:27:37 +0000 | [diff] [blame] | 1656 | acl->defaultDeny ? "deny" : "allow"); |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1657 | QTAILQ_FOREACH(entry, &acl->entries, next) { |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 1658 | i++; |
| 1659 | monitor_printf(mon, "%d: %s %s\n", i, |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 1660 | entry->deny ? "deny" : "allow", entry->match); |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 1661 | } |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 1662 | } |
| 1663 | } |
| 1664 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 1665 | static void hmp_acl_reset(Monitor *mon, const QDict *qdict) |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 1666 | { |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 1667 | const char *aclname = qdict_get_str(qdict, "aclname"); |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 1668 | qemu_acl *acl = find_acl(mon, aclname); |
| 1669 | |
| 1670 | if (acl) { |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 1671 | qemu_acl_reset(acl); |
| 1672 | monitor_printf(mon, "acl: removed all rules\n"); |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 1673 | } |
| 1674 | } |
aliguori | 76655d6 | 2009-03-06 20:27:37 +0000 | [diff] [blame] | 1675 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 1676 | static void hmp_acl_policy(Monitor *mon, const QDict *qdict) |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 1677 | { |
Luiz Capitulino | f18c16d | 2009-08-28 15:27:14 -0300 | [diff] [blame] | 1678 | const char *aclname = qdict_get_str(qdict, "aclname"); |
| 1679 | const char *policy = qdict_get_str(qdict, "policy"); |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 1680 | qemu_acl *acl = find_acl(mon, aclname); |
| 1681 | |
| 1682 | if (acl) { |
| 1683 | if (strcmp(policy, "allow") == 0) { |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 1684 | acl->defaultDeny = 0; |
| 1685 | monitor_printf(mon, "acl: policy set to 'allow'\n"); |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 1686 | } else if (strcmp(policy, "deny") == 0) { |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 1687 | acl->defaultDeny = 1; |
| 1688 | monitor_printf(mon, "acl: policy set to 'deny'\n"); |
| 1689 | } else { |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 1690 | monitor_printf(mon, "acl: unknown policy '%s', " |
| 1691 | "expected 'deny' or 'allow'\n", policy); |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 1692 | } |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 1693 | } |
| 1694 | } |
aliguori | 76655d6 | 2009-03-06 20:27:37 +0000 | [diff] [blame] | 1695 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 1696 | static void hmp_acl_add(Monitor *mon, const QDict *qdict) |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 1697 | { |
Luiz Capitulino | 1bd1442 | 2009-08-28 15:27:17 -0300 | [diff] [blame] | 1698 | const char *aclname = qdict_get_str(qdict, "aclname"); |
| 1699 | const char *match = qdict_get_str(qdict, "match"); |
| 1700 | const char *policy = qdict_get_str(qdict, "policy"); |
| 1701 | int has_index = qdict_haskey(qdict, "index"); |
| 1702 | int index = qdict_get_try_int(qdict, "index", -1); |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 1703 | qemu_acl *acl = find_acl(mon, aclname); |
| 1704 | int deny, ret; |
| 1705 | |
| 1706 | if (acl) { |
| 1707 | if (strcmp(policy, "allow") == 0) { |
| 1708 | deny = 0; |
| 1709 | } else if (strcmp(policy, "deny") == 0) { |
| 1710 | deny = 1; |
| 1711 | } else { |
| 1712 | monitor_printf(mon, "acl: unknown policy '%s', " |
| 1713 | "expected 'deny' or 'allow'\n", policy); |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 1714 | return; |
| 1715 | } |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 1716 | if (has_index) |
| 1717 | ret = qemu_acl_insert(acl, deny, match, index); |
| 1718 | else |
| 1719 | ret = qemu_acl_append(acl, deny, match); |
| 1720 | if (ret < 0) |
| 1721 | monitor_printf(mon, "acl: unable to add acl entry\n"); |
| 1722 | else |
| 1723 | monitor_printf(mon, "acl: added rule at position %d\n", ret); |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 1724 | } |
| 1725 | } |
aliguori | 76655d6 | 2009-03-06 20:27:37 +0000 | [diff] [blame] | 1726 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 1727 | static void hmp_acl_remove(Monitor *mon, const QDict *qdict) |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 1728 | { |
Luiz Capitulino | f18c16d | 2009-08-28 15:27:14 -0300 | [diff] [blame] | 1729 | const char *aclname = qdict_get_str(qdict, "aclname"); |
| 1730 | const char *match = qdict_get_str(qdict, "match"); |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 1731 | qemu_acl *acl = find_acl(mon, aclname); |
| 1732 | int ret; |
aliguori | 76655d6 | 2009-03-06 20:27:37 +0000 | [diff] [blame] | 1733 | |
Jan Kiszka | 15dfcd4 | 2009-06-25 08:22:08 +0200 | [diff] [blame] | 1734 | if (acl) { |
aliguori | 28a76be | 2009-03-06 20:27:40 +0000 | [diff] [blame] | 1735 | ret = qemu_acl_remove(acl, match); |
| 1736 | if (ret < 0) |
| 1737 | monitor_printf(mon, "acl: no matching acl entry\n"); |
| 1738 | else |
| 1739 | monitor_printf(mon, "acl: removed rule at position %d\n", ret); |
aliguori | 76655d6 | 2009-03-06 20:27:37 +0000 | [diff] [blame] | 1740 | } |
| 1741 | } |
| 1742 | |
Corey Bryant | 208c9d1 | 2012-06-22 14:36:09 -0400 | [diff] [blame] | 1743 | void qmp_getfd(const char *fdname, Error **errp) |
Mark McLoughlin | f07918f | 2009-07-22 09:11:40 +0100 | [diff] [blame] | 1744 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 1745 | mon_fd_t *monfd; |
Mark McLoughlin | f07918f | 2009-07-22 09:11:40 +0100 | [diff] [blame] | 1746 | int fd; |
| 1747 | |
Corey Bryant | 208c9d1 | 2012-06-22 14:36:09 -0400 | [diff] [blame] | 1748 | fd = qemu_chr_fe_get_msgfd(cur_mon->chr); |
Mark McLoughlin | f07918f | 2009-07-22 09:11:40 +0100 | [diff] [blame] | 1749 | if (fd == -1) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 1750 | error_setg(errp, QERR_FD_NOT_SUPPLIED); |
Corey Bryant | 208c9d1 | 2012-06-22 14:36:09 -0400 | [diff] [blame] | 1751 | return; |
Mark McLoughlin | f07918f | 2009-07-22 09:11:40 +0100 | [diff] [blame] | 1752 | } |
| 1753 | |
| 1754 | if (qemu_isdigit(fdname[0])) { |
Stefan Hajnoczi | 0b9f0e2 | 2014-04-24 13:58:18 +0200 | [diff] [blame] | 1755 | close(fd); |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 1756 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname", |
| 1757 | "a name not starting with a digit"); |
Corey Bryant | 208c9d1 | 2012-06-22 14:36:09 -0400 | [diff] [blame] | 1758 | return; |
Mark McLoughlin | f07918f | 2009-07-22 09:11:40 +0100 | [diff] [blame] | 1759 | } |
| 1760 | |
Corey Bryant | 208c9d1 | 2012-06-22 14:36:09 -0400 | [diff] [blame] | 1761 | QLIST_FOREACH(monfd, &cur_mon->fds, next) { |
Mark McLoughlin | f07918f | 2009-07-22 09:11:40 +0100 | [diff] [blame] | 1762 | if (strcmp(monfd->name, fdname) != 0) { |
| 1763 | continue; |
| 1764 | } |
| 1765 | |
| 1766 | close(monfd->fd); |
| 1767 | monfd->fd = fd; |
Corey Bryant | 208c9d1 | 2012-06-22 14:36:09 -0400 | [diff] [blame] | 1768 | return; |
Mark McLoughlin | f07918f | 2009-07-22 09:11:40 +0100 | [diff] [blame] | 1769 | } |
| 1770 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1771 | monfd = g_malloc0(sizeof(mon_fd_t)); |
| 1772 | monfd->name = g_strdup(fdname); |
Mark McLoughlin | f07918f | 2009-07-22 09:11:40 +0100 | [diff] [blame] | 1773 | monfd->fd = fd; |
| 1774 | |
Corey Bryant | 208c9d1 | 2012-06-22 14:36:09 -0400 | [diff] [blame] | 1775 | QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next); |
Mark McLoughlin | f07918f | 2009-07-22 09:11:40 +0100 | [diff] [blame] | 1776 | } |
| 1777 | |
Corey Bryant | 208c9d1 | 2012-06-22 14:36:09 -0400 | [diff] [blame] | 1778 | void qmp_closefd(const char *fdname, Error **errp) |
Mark McLoughlin | f07918f | 2009-07-22 09:11:40 +0100 | [diff] [blame] | 1779 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 1780 | mon_fd_t *monfd; |
Mark McLoughlin | f07918f | 2009-07-22 09:11:40 +0100 | [diff] [blame] | 1781 | |
Corey Bryant | 208c9d1 | 2012-06-22 14:36:09 -0400 | [diff] [blame] | 1782 | QLIST_FOREACH(monfd, &cur_mon->fds, next) { |
Mark McLoughlin | f07918f | 2009-07-22 09:11:40 +0100 | [diff] [blame] | 1783 | if (strcmp(monfd->name, fdname) != 0) { |
| 1784 | continue; |
| 1785 | } |
| 1786 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1787 | QLIST_REMOVE(monfd, next); |
Mark McLoughlin | f07918f | 2009-07-22 09:11:40 +0100 | [diff] [blame] | 1788 | close(monfd->fd); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1789 | g_free(monfd->name); |
| 1790 | g_free(monfd); |
Corey Bryant | 208c9d1 | 2012-06-22 14:36:09 -0400 | [diff] [blame] | 1791 | return; |
Mark McLoughlin | f07918f | 2009-07-22 09:11:40 +0100 | [diff] [blame] | 1792 | } |
| 1793 | |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 1794 | error_setg(errp, QERR_FD_NOT_FOUND, fdname); |
Mark McLoughlin | f07918f | 2009-07-22 09:11:40 +0100 | [diff] [blame] | 1795 | } |
| 1796 | |
Markus Armbruster | 3e5a50d | 2015-02-06 13:55:43 +0100 | [diff] [blame] | 1797 | static void hmp_loadvm(Monitor *mon, const QDict *qdict) |
Juan Quintela | c8d41b2 | 2009-08-20 19:42:21 +0200 | [diff] [blame] | 1798 | { |
Luiz Capitulino | 1354869 | 2011-07-29 15:36:43 -0300 | [diff] [blame] | 1799 | int saved_vm_running = runstate_is_running(); |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 1800 | const char *name = qdict_get_str(qdict, "name"); |
Juan Quintela | c8d41b2 | 2009-08-20 19:42:21 +0200 | [diff] [blame] | 1801 | |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 1802 | vm_stop(RUN_STATE_RESTORE_VM); |
Juan Quintela | c8d41b2 | 2009-08-20 19:42:21 +0200 | [diff] [blame] | 1803 | |
Miguel Di Ciurcio Filho | f0aa7a8 | 2010-07-19 15:25:01 -0300 | [diff] [blame] | 1804 | if (load_vmstate(name) == 0 && saved_vm_running) { |
Juan Quintela | c8d41b2 | 2009-08-20 19:42:21 +0200 | [diff] [blame] | 1805 | vm_start(); |
Miguel Di Ciurcio Filho | f0aa7a8 | 2010-07-19 15:25:01 -0300 | [diff] [blame] | 1806 | } |
Juan Quintela | c8d41b2 | 2009-08-20 19:42:21 +0200 | [diff] [blame] | 1807 | } |
| 1808 | |
Paolo Bonzini | a9940fc | 2012-09-20 16:50:32 +0200 | [diff] [blame] | 1809 | int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp) |
Mark McLoughlin | 7768e04 | 2009-07-22 09:11:41 +0100 | [diff] [blame] | 1810 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 1811 | mon_fd_t *monfd; |
Mark McLoughlin | 7768e04 | 2009-07-22 09:11:41 +0100 | [diff] [blame] | 1812 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1813 | QLIST_FOREACH(monfd, &mon->fds, next) { |
Mark McLoughlin | 7768e04 | 2009-07-22 09:11:41 +0100 | [diff] [blame] | 1814 | int fd; |
| 1815 | |
| 1816 | if (strcmp(monfd->name, fdname) != 0) { |
| 1817 | continue; |
| 1818 | } |
| 1819 | |
| 1820 | fd = monfd->fd; |
| 1821 | |
| 1822 | /* caller takes ownership of fd */ |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1823 | QLIST_REMOVE(monfd, next); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1824 | g_free(monfd->name); |
| 1825 | g_free(monfd); |
Mark McLoughlin | 7768e04 | 2009-07-22 09:11:41 +0100 | [diff] [blame] | 1826 | |
| 1827 | return fd; |
| 1828 | } |
| 1829 | |
Paolo Bonzini | a9940fc | 2012-09-20 16:50:32 +0200 | [diff] [blame] | 1830 | error_setg(errp, "File descriptor named '%s' has not been found", fdname); |
Mark McLoughlin | 7768e04 | 2009-07-22 09:11:41 +0100 | [diff] [blame] | 1831 | return -1; |
| 1832 | } |
| 1833 | |
Corey Bryant | ba1c048 | 2012-08-14 16:43:43 -0400 | [diff] [blame] | 1834 | static void monitor_fdset_cleanup(MonFdset *mon_fdset) |
| 1835 | { |
| 1836 | MonFdsetFd *mon_fdset_fd; |
| 1837 | MonFdsetFd *mon_fdset_fd_next; |
| 1838 | |
| 1839 | 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] | 1840 | if ((mon_fdset_fd->removed || |
| 1841 | (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) && |
| 1842 | runstate_is_running()) { |
Corey Bryant | ba1c048 | 2012-08-14 16:43:43 -0400 | [diff] [blame] | 1843 | close(mon_fdset_fd->fd); |
| 1844 | g_free(mon_fdset_fd->opaque); |
| 1845 | QLIST_REMOVE(mon_fdset_fd, next); |
| 1846 | g_free(mon_fdset_fd); |
| 1847 | } |
| 1848 | } |
| 1849 | |
Corey Bryant | adb696f | 2012-08-14 16:43:47 -0400 | [diff] [blame] | 1850 | if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) { |
Corey Bryant | ba1c048 | 2012-08-14 16:43:43 -0400 | [diff] [blame] | 1851 | QLIST_REMOVE(mon_fdset, next); |
| 1852 | g_free(mon_fdset); |
| 1853 | } |
| 1854 | } |
| 1855 | |
Corey Bryant | efb87c1 | 2012-08-14 16:43:48 -0400 | [diff] [blame] | 1856 | static void monitor_fdsets_cleanup(void) |
| 1857 | { |
| 1858 | MonFdset *mon_fdset; |
| 1859 | MonFdset *mon_fdset_next; |
| 1860 | |
| 1861 | QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) { |
| 1862 | monitor_fdset_cleanup(mon_fdset); |
| 1863 | } |
| 1864 | } |
| 1865 | |
Corey Bryant | ba1c048 | 2012-08-14 16:43:43 -0400 | [diff] [blame] | 1866 | AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque, |
| 1867 | const char *opaque, Error **errp) |
| 1868 | { |
| 1869 | int fd; |
| 1870 | Monitor *mon = cur_mon; |
Corey Bryant | ba1c048 | 2012-08-14 16:43:43 -0400 | [diff] [blame] | 1871 | AddfdInfo *fdinfo; |
| 1872 | |
| 1873 | fd = qemu_chr_fe_get_msgfd(mon->chr); |
| 1874 | if (fd == -1) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 1875 | error_setg(errp, QERR_FD_NOT_SUPPLIED); |
Corey Bryant | ba1c048 | 2012-08-14 16:43:43 -0400 | [diff] [blame] | 1876 | goto error; |
| 1877 | } |
| 1878 | |
Corey Bryant | e446f70 | 2012-10-18 15:19:32 -0400 | [diff] [blame] | 1879 | fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id, |
| 1880 | has_opaque, opaque, errp); |
| 1881 | if (fdinfo) { |
| 1882 | return fdinfo; |
Corey Bryant | 9ac54af | 2012-10-18 15:19:31 -0400 | [diff] [blame] | 1883 | } |
| 1884 | |
Corey Bryant | ba1c048 | 2012-08-14 16:43:43 -0400 | [diff] [blame] | 1885 | error: |
| 1886 | if (fd != -1) { |
| 1887 | close(fd); |
| 1888 | } |
| 1889 | return NULL; |
| 1890 | } |
| 1891 | |
| 1892 | void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp) |
| 1893 | { |
| 1894 | MonFdset *mon_fdset; |
| 1895 | MonFdsetFd *mon_fdset_fd; |
| 1896 | char fd_str[60]; |
| 1897 | |
| 1898 | QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { |
| 1899 | if (mon_fdset->id != fdset_id) { |
| 1900 | continue; |
| 1901 | } |
| 1902 | QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) { |
| 1903 | if (has_fd) { |
| 1904 | if (mon_fdset_fd->fd != fd) { |
| 1905 | continue; |
| 1906 | } |
| 1907 | mon_fdset_fd->removed = true; |
| 1908 | break; |
| 1909 | } else { |
| 1910 | mon_fdset_fd->removed = true; |
| 1911 | } |
| 1912 | } |
| 1913 | if (has_fd && !mon_fdset_fd) { |
| 1914 | goto error; |
| 1915 | } |
| 1916 | monitor_fdset_cleanup(mon_fdset); |
| 1917 | return; |
| 1918 | } |
| 1919 | |
| 1920 | error: |
| 1921 | if (has_fd) { |
| 1922 | snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64, |
| 1923 | fdset_id, fd); |
| 1924 | } else { |
| 1925 | snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id); |
| 1926 | } |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 1927 | error_setg(errp, QERR_FD_NOT_FOUND, fd_str); |
Corey Bryant | ba1c048 | 2012-08-14 16:43:43 -0400 | [diff] [blame] | 1928 | } |
| 1929 | |
| 1930 | FdsetInfoList *qmp_query_fdsets(Error **errp) |
| 1931 | { |
| 1932 | MonFdset *mon_fdset; |
| 1933 | MonFdsetFd *mon_fdset_fd; |
| 1934 | FdsetInfoList *fdset_list = NULL; |
| 1935 | |
| 1936 | QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { |
| 1937 | FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info)); |
| 1938 | FdsetFdInfoList *fdsetfd_list = NULL; |
| 1939 | |
| 1940 | fdset_info->value = g_malloc0(sizeof(*fdset_info->value)); |
| 1941 | fdset_info->value->fdset_id = mon_fdset->id; |
| 1942 | |
| 1943 | QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) { |
| 1944 | FdsetFdInfoList *fdsetfd_info; |
| 1945 | |
| 1946 | fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info)); |
| 1947 | fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value)); |
| 1948 | fdsetfd_info->value->fd = mon_fdset_fd->fd; |
| 1949 | if (mon_fdset_fd->opaque) { |
| 1950 | fdsetfd_info->value->has_opaque = true; |
| 1951 | fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque); |
| 1952 | } else { |
| 1953 | fdsetfd_info->value->has_opaque = false; |
| 1954 | } |
| 1955 | |
| 1956 | fdsetfd_info->next = fdsetfd_list; |
| 1957 | fdsetfd_list = fdsetfd_info; |
| 1958 | } |
| 1959 | |
| 1960 | fdset_info->value->fds = fdsetfd_list; |
| 1961 | |
| 1962 | fdset_info->next = fdset_list; |
| 1963 | fdset_list = fdset_info; |
| 1964 | } |
| 1965 | |
| 1966 | return fdset_list; |
| 1967 | } |
| 1968 | |
Corey Bryant | e446f70 | 2012-10-18 15:19:32 -0400 | [diff] [blame] | 1969 | AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id, |
| 1970 | bool has_opaque, const char *opaque, |
| 1971 | Error **errp) |
| 1972 | { |
| 1973 | MonFdset *mon_fdset = NULL; |
| 1974 | MonFdsetFd *mon_fdset_fd; |
| 1975 | AddfdInfo *fdinfo; |
| 1976 | |
| 1977 | if (has_fdset_id) { |
| 1978 | QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { |
| 1979 | /* Break if match found or match impossible due to ordering by ID */ |
| 1980 | if (fdset_id <= mon_fdset->id) { |
| 1981 | if (fdset_id < mon_fdset->id) { |
| 1982 | mon_fdset = NULL; |
| 1983 | } |
| 1984 | break; |
| 1985 | } |
| 1986 | } |
| 1987 | } |
| 1988 | |
| 1989 | if (mon_fdset == NULL) { |
| 1990 | int64_t fdset_id_prev = -1; |
| 1991 | MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets); |
| 1992 | |
| 1993 | if (has_fdset_id) { |
| 1994 | if (fdset_id < 0) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 1995 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id", |
| 1996 | "a non-negative value"); |
Corey Bryant | e446f70 | 2012-10-18 15:19:32 -0400 | [diff] [blame] | 1997 | return NULL; |
| 1998 | } |
| 1999 | /* Use specified fdset ID */ |
| 2000 | QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { |
| 2001 | mon_fdset_cur = mon_fdset; |
| 2002 | if (fdset_id < mon_fdset_cur->id) { |
| 2003 | break; |
| 2004 | } |
| 2005 | } |
| 2006 | } else { |
| 2007 | /* Use first available fdset ID */ |
| 2008 | QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { |
| 2009 | mon_fdset_cur = mon_fdset; |
| 2010 | if (fdset_id_prev == mon_fdset_cur->id - 1) { |
| 2011 | fdset_id_prev = mon_fdset_cur->id; |
| 2012 | continue; |
| 2013 | } |
| 2014 | break; |
| 2015 | } |
| 2016 | } |
| 2017 | |
| 2018 | mon_fdset = g_malloc0(sizeof(*mon_fdset)); |
| 2019 | if (has_fdset_id) { |
| 2020 | mon_fdset->id = fdset_id; |
| 2021 | } else { |
| 2022 | mon_fdset->id = fdset_id_prev + 1; |
| 2023 | } |
| 2024 | |
| 2025 | /* The fdset list is ordered by fdset ID */ |
| 2026 | if (!mon_fdset_cur) { |
| 2027 | QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next); |
| 2028 | } else if (mon_fdset->id < mon_fdset_cur->id) { |
| 2029 | QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next); |
| 2030 | } else { |
| 2031 | QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next); |
| 2032 | } |
| 2033 | } |
| 2034 | |
| 2035 | mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd)); |
| 2036 | mon_fdset_fd->fd = fd; |
| 2037 | mon_fdset_fd->removed = false; |
| 2038 | if (has_opaque) { |
| 2039 | mon_fdset_fd->opaque = g_strdup(opaque); |
| 2040 | } |
| 2041 | QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next); |
| 2042 | |
| 2043 | fdinfo = g_malloc0(sizeof(*fdinfo)); |
| 2044 | fdinfo->fdset_id = mon_fdset->id; |
| 2045 | fdinfo->fd = mon_fdset_fd->fd; |
| 2046 | |
| 2047 | return fdinfo; |
| 2048 | } |
| 2049 | |
Corey Bryant | adb696f | 2012-08-14 16:43:47 -0400 | [diff] [blame] | 2050 | int monitor_fdset_get_fd(int64_t fdset_id, int flags) |
| 2051 | { |
Blue Swirl | b2dc64c | 2012-08-18 20:14:54 +0000 | [diff] [blame] | 2052 | #ifndef _WIN32 |
Corey Bryant | adb696f | 2012-08-14 16:43:47 -0400 | [diff] [blame] | 2053 | MonFdset *mon_fdset; |
| 2054 | MonFdsetFd *mon_fdset_fd; |
| 2055 | int mon_fd_flags; |
| 2056 | |
Corey Bryant | adb696f | 2012-08-14 16:43:47 -0400 | [diff] [blame] | 2057 | QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { |
| 2058 | if (mon_fdset->id != fdset_id) { |
| 2059 | continue; |
| 2060 | } |
| 2061 | QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) { |
| 2062 | mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL); |
| 2063 | if (mon_fd_flags == -1) { |
| 2064 | return -1; |
| 2065 | } |
| 2066 | |
| 2067 | if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) { |
| 2068 | return mon_fdset_fd->fd; |
| 2069 | } |
| 2070 | } |
| 2071 | errno = EACCES; |
| 2072 | return -1; |
| 2073 | } |
| 2074 | #endif |
| 2075 | |
| 2076 | errno = ENOENT; |
| 2077 | return -1; |
| 2078 | } |
| 2079 | |
| 2080 | int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd) |
| 2081 | { |
| 2082 | MonFdset *mon_fdset; |
| 2083 | MonFdsetFd *mon_fdset_fd_dup; |
| 2084 | |
| 2085 | QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { |
| 2086 | if (mon_fdset->id != fdset_id) { |
| 2087 | continue; |
| 2088 | } |
| 2089 | QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) { |
| 2090 | if (mon_fdset_fd_dup->fd == dup_fd) { |
| 2091 | return -1; |
| 2092 | } |
| 2093 | } |
| 2094 | mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup)); |
| 2095 | mon_fdset_fd_dup->fd = dup_fd; |
| 2096 | QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next); |
| 2097 | return 0; |
| 2098 | } |
| 2099 | return -1; |
| 2100 | } |
| 2101 | |
| 2102 | static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove) |
| 2103 | { |
| 2104 | MonFdset *mon_fdset; |
| 2105 | MonFdsetFd *mon_fdset_fd_dup; |
| 2106 | |
| 2107 | QLIST_FOREACH(mon_fdset, &mon_fdsets, next) { |
| 2108 | QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) { |
| 2109 | if (mon_fdset_fd_dup->fd == dup_fd) { |
| 2110 | if (remove) { |
| 2111 | QLIST_REMOVE(mon_fdset_fd_dup, next); |
| 2112 | if (QLIST_EMPTY(&mon_fdset->dup_fds)) { |
| 2113 | monitor_fdset_cleanup(mon_fdset); |
| 2114 | } |
Michael S. Tsirkin | b3dd1b8 | 2014-08-17 11:45:17 +0200 | [diff] [blame] | 2115 | return -1; |
| 2116 | } else { |
| 2117 | return mon_fdset->id; |
Corey Bryant | adb696f | 2012-08-14 16:43:47 -0400 | [diff] [blame] | 2118 | } |
Corey Bryant | adb696f | 2012-08-14 16:43:47 -0400 | [diff] [blame] | 2119 | } |
| 2120 | } |
| 2121 | } |
| 2122 | return -1; |
| 2123 | } |
| 2124 | |
| 2125 | int monitor_fdset_dup_fd_find(int dup_fd) |
| 2126 | { |
| 2127 | return monitor_fdset_dup_fd_find_remove(dup_fd, false); |
| 2128 | } |
| 2129 | |
Michael S. Tsirkin | b3dd1b8 | 2014-08-17 11:45:17 +0200 | [diff] [blame] | 2130 | void monitor_fdset_dup_fd_remove(int dup_fd) |
Corey Bryant | adb696f | 2012-08-14 16:43:47 -0400 | [diff] [blame] | 2131 | { |
Michael S. Tsirkin | b3dd1b8 | 2014-08-17 11:45:17 +0200 | [diff] [blame] | 2132 | monitor_fdset_dup_fd_find_remove(dup_fd, true); |
Corey Bryant | adb696f | 2012-08-14 16:43:47 -0400 | [diff] [blame] | 2133 | } |
| 2134 | |
Markus Armbruster | 1677f4c | 2015-02-09 14:03:19 +0100 | [diff] [blame] | 2135 | int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp) |
Laszlo Ersek | 5906366 | 2014-04-10 10:24:31 +0200 | [diff] [blame] | 2136 | { |
| 2137 | int fd; |
| 2138 | Error *local_err = NULL; |
| 2139 | |
| 2140 | if (!qemu_isdigit(fdname[0]) && mon) { |
Paolo Bonzini | a9940fc | 2012-09-20 16:50:32 +0200 | [diff] [blame] | 2141 | fd = monitor_get_fd(mon, fdname, &local_err); |
Nicholas Bellinger | a96ed02 | 2012-08-21 20:52:07 +0000 | [diff] [blame] | 2142 | } else { |
| 2143 | fd = qemu_parse_fd(fdname); |
Laszlo Ersek | 5906366 | 2014-04-10 10:24:31 +0200 | [diff] [blame] | 2144 | if (fd == -1) { |
| 2145 | error_setg(&local_err, "Invalid file descriptor number '%s'", |
| 2146 | fdname); |
| 2147 | } |
| 2148 | } |
| 2149 | if (local_err) { |
| 2150 | error_propagate(errp, local_err); |
| 2151 | assert(fd == -1); |
| 2152 | } else { |
| 2153 | assert(fd != -1); |
Nicholas Bellinger | a96ed02 | 2012-08-21 20:52:07 +0000 | [diff] [blame] | 2154 | } |
| 2155 | |
| 2156 | return fd; |
| 2157 | } |
| 2158 | |
Luiz Capitulino | acd0a09 | 2010-09-30 16:00:22 -0300 | [diff] [blame] | 2159 | /* Please update hmp-commands.hx when adding or changing commands */ |
Wayne Xia | 816f892 | 2011-10-12 11:32:41 +0800 | [diff] [blame] | 2160 | static mon_cmd_t info_cmds[] = { |
Pavel Butsykin | da76ee7 | 2015-09-10 18:38:58 +0300 | [diff] [blame] | 2161 | #include "hmp-commands-info.h" |
| 2162 | { NULL, NULL, }, |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 2163 | }; |
| 2164 | |
Wenchao Xia | a13ced5 | 2013-01-14 14:06:28 +0800 | [diff] [blame] | 2165 | /* mon_cmds and info_cmds would be sorted at runtime */ |
| 2166 | static mon_cmd_t mon_cmds[] = { |
| 2167 | #include "hmp-commands.h" |
| 2168 | { NULL, NULL, }, |
| 2169 | }; |
| 2170 | |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2171 | /*******************************************************************/ |
| 2172 | |
| 2173 | static const char *pch; |
Peter Maydell | 6ab7e54 | 2013-02-20 15:21:09 +0000 | [diff] [blame] | 2174 | static sigjmp_buf expr_env; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2175 | |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2176 | |
Stefan Weil | 9c3175c | 2013-08-22 21:30:09 +0200 | [diff] [blame] | 2177 | static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN |
| 2178 | expr_error(Monitor *mon, const char *fmt, ...) |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 2179 | { |
Fam Zheng | 277acfe | 2013-08-20 10:58:21 +0800 | [diff] [blame] | 2180 | va_list ap; |
| 2181 | va_start(ap, fmt); |
| 2182 | monitor_vprintf(mon, fmt, ap); |
| 2183 | monitor_printf(mon, "\n"); |
| 2184 | va_end(ap); |
Peter Maydell | 6ab7e54 | 2013-02-20 15:21:09 +0000 | [diff] [blame] | 2185 | siglongjmp(expr_env, 1); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2186 | } |
| 2187 | |
Markus Armbruster | 09b9418 | 2010-01-20 13:07:30 +0100 | [diff] [blame] | 2188 | /* return 0 if OK, -1 if not found */ |
bellard | 92a31b1 | 2005-02-10 22:00:52 +0000 | [diff] [blame] | 2189 | static int get_monitor_def(target_long *pval, const char *name) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2190 | { |
Pavel Butsykin | bf95728 | 2015-09-10 18:38:59 +0300 | [diff] [blame] | 2191 | const MonitorDef *md = target_monitor_defs(); |
bellard | 92a31b1 | 2005-02-10 22:00:52 +0000 | [diff] [blame] | 2192 | void *ptr; |
Alexey Kardashevskiy | 0a9516c | 2015-11-12 14:44:23 +1100 | [diff] [blame] | 2193 | uint64_t tmp = 0; |
| 2194 | int ret; |
bellard | 92a31b1 | 2005-02-10 22:00:52 +0000 | [diff] [blame] | 2195 | |
Pavel Butsykin | bf95728 | 2015-09-10 18:38:59 +0300 | [diff] [blame] | 2196 | if (md == NULL) { |
| 2197 | return -1; |
| 2198 | } |
| 2199 | |
| 2200 | for(; md->name != NULL; md++) { |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2201 | if (compare_cmd(name, md->name)) { |
| 2202 | if (md->get_value) { |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 2203 | *pval = md->get_value(md, md->offset); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2204 | } else { |
Peter Crosthwaite | 5bcda5f | 2015-05-24 14:20:40 -0700 | [diff] [blame] | 2205 | CPUArchState *env = mon_get_cpu_env(); |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 2206 | ptr = (uint8_t *)env + md->offset; |
bellard | 92a31b1 | 2005-02-10 22:00:52 +0000 | [diff] [blame] | 2207 | switch(md->type) { |
| 2208 | case MD_I32: |
| 2209 | *pval = *(int32_t *)ptr; |
| 2210 | break; |
| 2211 | case MD_TLONG: |
| 2212 | *pval = *(target_long *)ptr; |
| 2213 | break; |
| 2214 | default: |
| 2215 | *pval = 0; |
| 2216 | break; |
| 2217 | } |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2218 | } |
| 2219 | return 0; |
| 2220 | } |
| 2221 | } |
Alexey Kardashevskiy | 0a9516c | 2015-11-12 14:44:23 +1100 | [diff] [blame] | 2222 | |
| 2223 | ret = target_get_monitor_def(mon_get_cpu(), name, &tmp); |
| 2224 | if (!ret) { |
| 2225 | *pval = (target_long) tmp; |
| 2226 | } |
| 2227 | |
| 2228 | return ret; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2229 | } |
| 2230 | |
| 2231 | static void next(void) |
| 2232 | { |
Blue Swirl | 660f11b | 2009-07-31 21:16:51 +0000 | [diff] [blame] | 2233 | if (*pch != '\0') { |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2234 | pch++; |
blueswir1 | cd39008 | 2008-11-16 13:53:32 +0000 | [diff] [blame] | 2235 | while (qemu_isspace(*pch)) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2236 | pch++; |
| 2237 | } |
| 2238 | } |
| 2239 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2240 | static int64_t expr_sum(Monitor *mon); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2241 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2242 | static int64_t expr_unary(Monitor *mon) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2243 | { |
blueswir1 | c2efc95 | 2007-09-25 17:28:42 +0000 | [diff] [blame] | 2244 | int64_t n; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2245 | char *p; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 2246 | int ret; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2247 | |
| 2248 | switch(*pch) { |
| 2249 | case '+': |
| 2250 | next(); |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2251 | n = expr_unary(mon); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2252 | break; |
| 2253 | case '-': |
| 2254 | next(); |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2255 | n = -expr_unary(mon); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2256 | break; |
| 2257 | case '~': |
| 2258 | next(); |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2259 | n = ~expr_unary(mon); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2260 | break; |
| 2261 | case '(': |
| 2262 | next(); |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2263 | n = expr_sum(mon); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2264 | if (*pch != ')') { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2265 | expr_error(mon, "')' expected"); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2266 | } |
| 2267 | next(); |
| 2268 | break; |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 2269 | case '\'': |
| 2270 | pch++; |
| 2271 | if (*pch == '\0') |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2272 | expr_error(mon, "character constant expected"); |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 2273 | n = *pch; |
| 2274 | pch++; |
| 2275 | if (*pch != '\'') |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2276 | expr_error(mon, "missing terminating \' character"); |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 2277 | next(); |
| 2278 | break; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2279 | case '$': |
| 2280 | { |
| 2281 | char buf[128], *q; |
ths | 69b3497 | 2007-12-17 03:15:52 +0000 | [diff] [blame] | 2282 | target_long reg=0; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2283 | |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2284 | pch++; |
| 2285 | q = buf; |
| 2286 | while ((*pch >= 'a' && *pch <= 'z') || |
| 2287 | (*pch >= 'A' && *pch <= 'Z') || |
| 2288 | (*pch >= '0' && *pch <= '9') || |
bellard | 57206fd | 2004-04-25 18:54:52 +0000 | [diff] [blame] | 2289 | *pch == '_' || *pch == '.') { |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2290 | if ((q - buf) < sizeof(buf) - 1) |
| 2291 | *q++ = *pch; |
| 2292 | pch++; |
| 2293 | } |
blueswir1 | cd39008 | 2008-11-16 13:53:32 +0000 | [diff] [blame] | 2294 | while (qemu_isspace(*pch)) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2295 | pch++; |
| 2296 | *q = 0; |
blueswir1 | 7743e58 | 2007-09-24 18:39:04 +0000 | [diff] [blame] | 2297 | ret = get_monitor_def(®, buf); |
Markus Armbruster | 09b9418 | 2010-01-20 13:07:30 +0100 | [diff] [blame] | 2298 | if (ret < 0) |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2299 | expr_error(mon, "unknown register"); |
blueswir1 | 7743e58 | 2007-09-24 18:39:04 +0000 | [diff] [blame] | 2300 | n = reg; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2301 | } |
| 2302 | break; |
| 2303 | case '\0': |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2304 | expr_error(mon, "unexpected end of expression"); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2305 | n = 0; |
| 2306 | break; |
| 2307 | default: |
Luiz Capitulino | 6b0e33b | 2012-04-26 16:48:41 -0300 | [diff] [blame] | 2308 | errno = 0; |
bellard | 4f4fbf7 | 2006-06-25 18:28:12 +0000 | [diff] [blame] | 2309 | n = strtoull(pch, &p, 0); |
Luiz Capitulino | 6b0e33b | 2012-04-26 16:48:41 -0300 | [diff] [blame] | 2310 | if (errno == ERANGE) { |
| 2311 | expr_error(mon, "number too large"); |
| 2312 | } |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2313 | if (pch == p) { |
Fam Zheng | 277acfe | 2013-08-20 10:58:21 +0800 | [diff] [blame] | 2314 | expr_error(mon, "invalid char '%c' in expression", *p); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2315 | } |
| 2316 | pch = p; |
blueswir1 | cd39008 | 2008-11-16 13:53:32 +0000 | [diff] [blame] | 2317 | while (qemu_isspace(*pch)) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2318 | pch++; |
| 2319 | break; |
| 2320 | } |
| 2321 | return n; |
| 2322 | } |
| 2323 | |
| 2324 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2325 | static int64_t expr_prod(Monitor *mon) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2326 | { |
blueswir1 | c2efc95 | 2007-09-25 17:28:42 +0000 | [diff] [blame] | 2327 | int64_t val, val2; |
bellard | 92a31b1 | 2005-02-10 22:00:52 +0000 | [diff] [blame] | 2328 | int op; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2329 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2330 | val = expr_unary(mon); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2331 | for(;;) { |
| 2332 | op = *pch; |
| 2333 | if (op != '*' && op != '/' && op != '%') |
| 2334 | break; |
| 2335 | next(); |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2336 | val2 = expr_unary(mon); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2337 | switch(op) { |
| 2338 | default: |
| 2339 | case '*': |
| 2340 | val *= val2; |
| 2341 | break; |
| 2342 | case '/': |
| 2343 | case '%': |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2344 | if (val2 == 0) |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2345 | expr_error(mon, "division by zero"); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2346 | if (op == '/') |
| 2347 | val /= val2; |
| 2348 | else |
| 2349 | val %= val2; |
| 2350 | break; |
| 2351 | } |
| 2352 | } |
| 2353 | return val; |
| 2354 | } |
| 2355 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2356 | static int64_t expr_logic(Monitor *mon) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2357 | { |
blueswir1 | c2efc95 | 2007-09-25 17:28:42 +0000 | [diff] [blame] | 2358 | int64_t val, val2; |
bellard | 92a31b1 | 2005-02-10 22:00:52 +0000 | [diff] [blame] | 2359 | int op; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2360 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2361 | val = expr_prod(mon); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2362 | for(;;) { |
| 2363 | op = *pch; |
| 2364 | if (op != '&' && op != '|' && op != '^') |
| 2365 | break; |
| 2366 | next(); |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2367 | val2 = expr_prod(mon); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2368 | switch(op) { |
| 2369 | default: |
| 2370 | case '&': |
| 2371 | val &= val2; |
| 2372 | break; |
| 2373 | case '|': |
| 2374 | val |= val2; |
| 2375 | break; |
| 2376 | case '^': |
| 2377 | val ^= val2; |
| 2378 | break; |
| 2379 | } |
| 2380 | } |
| 2381 | return val; |
| 2382 | } |
| 2383 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2384 | static int64_t expr_sum(Monitor *mon) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2385 | { |
blueswir1 | c2efc95 | 2007-09-25 17:28:42 +0000 | [diff] [blame] | 2386 | int64_t val, val2; |
bellard | 92a31b1 | 2005-02-10 22:00:52 +0000 | [diff] [blame] | 2387 | int op; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2388 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2389 | val = expr_logic(mon); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2390 | for(;;) { |
| 2391 | op = *pch; |
| 2392 | if (op != '+' && op != '-') |
| 2393 | break; |
| 2394 | next(); |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2395 | val2 = expr_logic(mon); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2396 | if (op == '+') |
| 2397 | val += val2; |
| 2398 | else |
| 2399 | val -= val2; |
| 2400 | } |
| 2401 | return val; |
| 2402 | } |
| 2403 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2404 | static int get_expr(Monitor *mon, int64_t *pval, const char **pp) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2405 | { |
| 2406 | pch = *pp; |
Peter Maydell | 6ab7e54 | 2013-02-20 15:21:09 +0000 | [diff] [blame] | 2407 | if (sigsetjmp(expr_env, 0)) { |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2408 | *pp = pch; |
| 2409 | return -1; |
| 2410 | } |
blueswir1 | cd39008 | 2008-11-16 13:53:32 +0000 | [diff] [blame] | 2411 | while (qemu_isspace(*pch)) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2412 | pch++; |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2413 | *pval = expr_sum(mon); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2414 | *pp = pch; |
| 2415 | return 0; |
| 2416 | } |
| 2417 | |
Markus Armbruster | 3350a4d | 2010-01-25 14:23:03 +0100 | [diff] [blame] | 2418 | static int get_double(Monitor *mon, double *pval, const char **pp) |
| 2419 | { |
| 2420 | const char *p = *pp; |
| 2421 | char *tailp; |
| 2422 | double d; |
| 2423 | |
| 2424 | d = strtod(p, &tailp); |
| 2425 | if (tailp == p) { |
| 2426 | monitor_printf(mon, "Number expected\n"); |
| 2427 | return -1; |
| 2428 | } |
| 2429 | if (d != d || d - d != 0) { |
| 2430 | /* NaN or infinity */ |
| 2431 | monitor_printf(mon, "Bad number\n"); |
| 2432 | return -1; |
| 2433 | } |
| 2434 | *pval = d; |
| 2435 | *pp = tailp; |
| 2436 | return 0; |
| 2437 | } |
| 2438 | |
Luiz Capitulino | 4590fd8 | 2009-06-09 18:21:30 -0300 | [diff] [blame] | 2439 | /* |
| 2440 | * Store the command-name in cmdname, and return a pointer to |
| 2441 | * the remaining of the command string. |
| 2442 | */ |
| 2443 | static const char *get_command_name(const char *cmdline, |
| 2444 | char *cmdname, size_t nlen) |
| 2445 | { |
| 2446 | size_t len; |
| 2447 | const char *p, *pstart; |
| 2448 | |
| 2449 | p = cmdline; |
| 2450 | while (qemu_isspace(*p)) |
| 2451 | p++; |
| 2452 | if (*p == '\0') |
| 2453 | return NULL; |
| 2454 | pstart = p; |
| 2455 | while (*p != '\0' && *p != '/' && !qemu_isspace(*p)) |
| 2456 | p++; |
| 2457 | len = p - pstart; |
| 2458 | if (len > nlen - 1) |
| 2459 | len = nlen - 1; |
| 2460 | memcpy(cmdname, pstart, len); |
| 2461 | cmdname[len] = '\0'; |
| 2462 | return p; |
| 2463 | } |
| 2464 | |
Luiz Capitulino | 4d76d2b | 2009-08-28 15:27:09 -0300 | [diff] [blame] | 2465 | /** |
| 2466 | * Read key of 'type' into 'key' and return the current |
| 2467 | * 'type' pointer. |
| 2468 | */ |
| 2469 | static char *key_get_info(const char *type, char **key) |
| 2470 | { |
| 2471 | size_t len; |
| 2472 | char *p, *str; |
| 2473 | |
| 2474 | if (*type == ',') |
| 2475 | type++; |
| 2476 | |
| 2477 | p = strchr(type, ':'); |
| 2478 | if (!p) { |
| 2479 | *key = NULL; |
| 2480 | return NULL; |
| 2481 | } |
| 2482 | len = p - type; |
| 2483 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2484 | str = g_malloc(len + 1); |
Luiz Capitulino | 4d76d2b | 2009-08-28 15:27:09 -0300 | [diff] [blame] | 2485 | memcpy(str, type, len); |
| 2486 | str[len] = '\0'; |
| 2487 | |
| 2488 | *key = str; |
| 2489 | return ++p; |
| 2490 | } |
| 2491 | |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2492 | static int default_fmt_format = 'x'; |
| 2493 | static int default_fmt_size = 4; |
| 2494 | |
lirans@il.ibm.com | fbc3d96 | 2009-11-02 15:41:13 +0200 | [diff] [blame] | 2495 | static int is_valid_option(const char *c, const char *typestr) |
| 2496 | { |
| 2497 | char option[3]; |
| 2498 | |
| 2499 | option[0] = '-'; |
| 2500 | option[1] = *c; |
| 2501 | option[2] = '\0'; |
| 2502 | |
| 2503 | typestr = strstr(typestr, option); |
| 2504 | return (typestr != NULL); |
| 2505 | } |
| 2506 | |
Luiz Capitulino | 945c5ac | 2010-09-13 13:17:58 -0300 | [diff] [blame] | 2507 | static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table, |
| 2508 | const char *cmdname) |
Luiz Capitulino | 7fd669a | 2009-11-26 22:58:54 -0200 | [diff] [blame] | 2509 | { |
| 2510 | const mon_cmd_t *cmd; |
| 2511 | |
Luiz Capitulino | 945c5ac | 2010-09-13 13:17:58 -0300 | [diff] [blame] | 2512 | for (cmd = disp_table; cmd->name != NULL; cmd++) { |
Luiz Capitulino | 7fd669a | 2009-11-26 22:58:54 -0200 | [diff] [blame] | 2513 | if (compare_cmd(cmdname, cmd->name)) { |
| 2514 | return cmd; |
| 2515 | } |
| 2516 | } |
| 2517 | |
| 2518 | return NULL; |
| 2519 | } |
| 2520 | |
Wenchao Xia | 5f3d335 | 2013-01-14 14:06:27 +0800 | [diff] [blame] | 2521 | /* |
Bandan Das | ae50212 | 2015-06-03 18:38:08 -0400 | [diff] [blame] | 2522 | * Parse command name from @cmdp according to command table @table. |
| 2523 | * If blank, return NULL. |
| 2524 | * Else, if no valid command can be found, report to @mon, and return |
| 2525 | * NULL. |
| 2526 | * Else, change @cmdp to point right behind the name, and return its |
| 2527 | * command table entry. |
| 2528 | * Do not assume the return value points into @table! It doesn't when |
| 2529 | * the command is found in a sub-command table. |
Wenchao Xia | 5f3d335 | 2013-01-14 14:06:27 +0800 | [diff] [blame] | 2530 | */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2531 | static const mon_cmd_t *monitor_parse_command(Monitor *mon, |
Bandan Das | ae50212 | 2015-06-03 18:38:08 -0400 | [diff] [blame] | 2532 | const char **cmdp, |
| 2533 | mon_cmd_t *table) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2534 | { |
Bandan Das | ae50212 | 2015-06-03 18:38:08 -0400 | [diff] [blame] | 2535 | const char *p; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2536 | const mon_cmd_t *cmd; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2537 | char cmdname[256]; |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 2538 | |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2539 | /* extract the command name */ |
Bandan Das | ae50212 | 2015-06-03 18:38:08 -0400 | [diff] [blame] | 2540 | p = get_command_name(*cmdp, cmdname, sizeof(cmdname)); |
Luiz Capitulino | 4590fd8 | 2009-06-09 18:21:30 -0300 | [diff] [blame] | 2541 | if (!p) |
Luiz Capitulino | 55f81d9 | 2009-08-28 15:27:22 -0300 | [diff] [blame] | 2542 | return NULL; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2543 | |
Wenchao Xia | 5f3d335 | 2013-01-14 14:06:27 +0800 | [diff] [blame] | 2544 | cmd = search_dispatch_table(table, cmdname); |
Luiz Capitulino | 7fd669a | 2009-11-26 22:58:54 -0200 | [diff] [blame] | 2545 | if (!cmd) { |
Wenchao Xia | 5f3d335 | 2013-01-14 14:06:27 +0800 | [diff] [blame] | 2546 | monitor_printf(mon, "unknown command: '%.*s'\n", |
Bandan Das | ae50212 | 2015-06-03 18:38:08 -0400 | [diff] [blame] | 2547 | (int)(p - *cmdp), *cmdp); |
Luiz Capitulino | 55f81d9 | 2009-08-28 15:27:22 -0300 | [diff] [blame] | 2548 | return NULL; |
Luiz Capitulino | d91d9bf | 2009-06-09 18:21:54 -0300 | [diff] [blame] | 2549 | } |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2550 | |
Wenchao Xia | 5f3d335 | 2013-01-14 14:06:27 +0800 | [diff] [blame] | 2551 | /* filter out following useless space */ |
| 2552 | while (qemu_isspace(*p)) { |
| 2553 | p++; |
| 2554 | } |
Bandan Das | ae50212 | 2015-06-03 18:38:08 -0400 | [diff] [blame] | 2555 | |
| 2556 | *cmdp = p; |
Wenchao Xia | 5f3d335 | 2013-01-14 14:06:27 +0800 | [diff] [blame] | 2557 | /* search sub command */ |
Bandan Das | ae50212 | 2015-06-03 18:38:08 -0400 | [diff] [blame] | 2558 | if (cmd->sub_table != NULL && *p != '\0') { |
| 2559 | return monitor_parse_command(mon, cmdp, cmd->sub_table); |
Wenchao Xia | 5f3d335 | 2013-01-14 14:06:27 +0800 | [diff] [blame] | 2560 | } |
| 2561 | |
Bandan Das | ae50212 | 2015-06-03 18:38:08 -0400 | [diff] [blame] | 2562 | return cmd; |
| 2563 | } |
| 2564 | |
| 2565 | /* |
| 2566 | * Parse arguments for @cmd. |
| 2567 | * If it can't be parsed, report to @mon, and return NULL. |
| 2568 | * Else, insert command arguments into a QDict, and return it. |
| 2569 | * Note: On success, caller has to free the QDict structure. |
| 2570 | */ |
| 2571 | |
| 2572 | static QDict *monitor_parse_arguments(Monitor *mon, |
| 2573 | const char **endp, |
| 2574 | const mon_cmd_t *cmd) |
| 2575 | { |
| 2576 | const char *typestr; |
| 2577 | char *key; |
| 2578 | int c; |
| 2579 | const char *p = *endp; |
| 2580 | char buf[1024]; |
| 2581 | QDict *qdict = qdict_new(); |
| 2582 | |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2583 | /* parse the parameters */ |
| 2584 | typestr = cmd->args_type; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2585 | for(;;) { |
Luiz Capitulino | 4d76d2b | 2009-08-28 15:27:09 -0300 | [diff] [blame] | 2586 | typestr = key_get_info(typestr, &key); |
| 2587 | if (!typestr) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2588 | break; |
Luiz Capitulino | 4d76d2b | 2009-08-28 15:27:09 -0300 | [diff] [blame] | 2589 | c = *typestr; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2590 | typestr++; |
| 2591 | switch(c) { |
| 2592 | case 'F': |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 2593 | case 'B': |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2594 | case 's': |
| 2595 | { |
| 2596 | int ret; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2597 | |
blueswir1 | cd39008 | 2008-11-16 13:53:32 +0000 | [diff] [blame] | 2598 | while (qemu_isspace(*p)) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2599 | p++; |
| 2600 | if (*typestr == '?') { |
| 2601 | typestr++; |
| 2602 | if (*p == '\0') { |
| 2603 | /* no optional string: NULL argument */ |
Luiz Capitulino | 5377358 | 2009-08-28 15:27:25 -0300 | [diff] [blame] | 2604 | break; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2605 | } |
| 2606 | } |
| 2607 | ret = get_str(buf, sizeof(buf), &p); |
| 2608 | if (ret < 0) { |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 2609 | switch(c) { |
| 2610 | case 'F': |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2611 | monitor_printf(mon, "%s: filename expected\n", |
Bandan Das | ae50212 | 2015-06-03 18:38:08 -0400 | [diff] [blame] | 2612 | cmd->name); |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 2613 | break; |
| 2614 | case 'B': |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2615 | monitor_printf(mon, "%s: block device name expected\n", |
Bandan Das | ae50212 | 2015-06-03 18:38:08 -0400 | [diff] [blame] | 2616 | cmd->name); |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 2617 | break; |
| 2618 | default: |
Bandan Das | ae50212 | 2015-06-03 18:38:08 -0400 | [diff] [blame] | 2619 | monitor_printf(mon, "%s: string expected\n", cmd->name); |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 2620 | break; |
| 2621 | } |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2622 | goto fail; |
| 2623 | } |
Luiz Capitulino | 5377358 | 2009-08-28 15:27:25 -0300 | [diff] [blame] | 2624 | qdict_put(qdict, key, qstring_from_str(buf)); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2625 | } |
| 2626 | break; |
Markus Armbruster | 361127d | 2010-02-10 20:24:35 +0100 | [diff] [blame] | 2627 | case 'O': |
| 2628 | { |
| 2629 | QemuOptsList *opts_list; |
| 2630 | QemuOpts *opts; |
| 2631 | |
| 2632 | opts_list = qemu_find_opts(key); |
| 2633 | if (!opts_list || opts_list->desc->name) { |
| 2634 | goto bad_type; |
| 2635 | } |
| 2636 | while (qemu_isspace(*p)) { |
| 2637 | p++; |
| 2638 | } |
| 2639 | if (!*p) |
| 2640 | break; |
| 2641 | if (get_str(buf, sizeof(buf), &p) < 0) { |
| 2642 | goto fail; |
| 2643 | } |
Markus Armbruster | 70b9433 | 2015-02-13 12:50:26 +0100 | [diff] [blame] | 2644 | opts = qemu_opts_parse_noisily(opts_list, buf, true); |
Markus Armbruster | 361127d | 2010-02-10 20:24:35 +0100 | [diff] [blame] | 2645 | if (!opts) { |
| 2646 | goto fail; |
| 2647 | } |
| 2648 | qemu_opts_to_qdict(opts, qdict); |
| 2649 | qemu_opts_del(opts); |
| 2650 | } |
| 2651 | break; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2652 | case '/': |
| 2653 | { |
| 2654 | int count, format, size; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2655 | |
blueswir1 | cd39008 | 2008-11-16 13:53:32 +0000 | [diff] [blame] | 2656 | while (qemu_isspace(*p)) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2657 | p++; |
| 2658 | if (*p == '/') { |
| 2659 | /* format found */ |
| 2660 | p++; |
| 2661 | count = 1; |
blueswir1 | cd39008 | 2008-11-16 13:53:32 +0000 | [diff] [blame] | 2662 | if (qemu_isdigit(*p)) { |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2663 | count = 0; |
blueswir1 | cd39008 | 2008-11-16 13:53:32 +0000 | [diff] [blame] | 2664 | while (qemu_isdigit(*p)) { |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2665 | count = count * 10 + (*p - '0'); |
| 2666 | p++; |
| 2667 | } |
| 2668 | } |
| 2669 | size = -1; |
| 2670 | format = -1; |
| 2671 | for(;;) { |
| 2672 | switch(*p) { |
| 2673 | case 'o': |
| 2674 | case 'd': |
| 2675 | case 'u': |
| 2676 | case 'x': |
| 2677 | case 'i': |
| 2678 | case 'c': |
| 2679 | format = *p++; |
| 2680 | break; |
| 2681 | case 'b': |
| 2682 | size = 1; |
| 2683 | p++; |
| 2684 | break; |
| 2685 | case 'h': |
| 2686 | size = 2; |
| 2687 | p++; |
| 2688 | break; |
| 2689 | case 'w': |
| 2690 | size = 4; |
| 2691 | p++; |
| 2692 | break; |
| 2693 | case 'g': |
| 2694 | case 'L': |
| 2695 | size = 8; |
| 2696 | p++; |
| 2697 | break; |
| 2698 | default: |
| 2699 | goto next; |
| 2700 | } |
| 2701 | } |
| 2702 | next: |
blueswir1 | cd39008 | 2008-11-16 13:53:32 +0000 | [diff] [blame] | 2703 | if (*p != '\0' && !qemu_isspace(*p)) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2704 | monitor_printf(mon, "invalid char in format: '%c'\n", |
| 2705 | *p); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2706 | goto fail; |
| 2707 | } |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2708 | if (format < 0) |
| 2709 | format = default_fmt_format; |
bellard | 4c27ba2 | 2004-04-25 18:05:08 +0000 | [diff] [blame] | 2710 | if (format != 'i') { |
| 2711 | /* for 'i', not specifying a size gives -1 as size */ |
| 2712 | if (size < 0) |
| 2713 | size = default_fmt_size; |
aurel32 | e90f009 | 2008-10-01 21:45:51 +0000 | [diff] [blame] | 2714 | default_fmt_size = size; |
bellard | 4c27ba2 | 2004-04-25 18:05:08 +0000 | [diff] [blame] | 2715 | } |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2716 | default_fmt_format = format; |
| 2717 | } else { |
| 2718 | count = 1; |
| 2719 | format = default_fmt_format; |
bellard | 4c27ba2 | 2004-04-25 18:05:08 +0000 | [diff] [blame] | 2720 | if (format != 'i') { |
| 2721 | size = default_fmt_size; |
| 2722 | } else { |
| 2723 | size = -1; |
| 2724 | } |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2725 | } |
Luiz Capitulino | f7188bb | 2009-08-28 15:27:10 -0300 | [diff] [blame] | 2726 | qdict_put(qdict, "count", qint_from_int(count)); |
| 2727 | qdict_put(qdict, "format", qint_from_int(format)); |
| 2728 | qdict_put(qdict, "size", qint_from_int(size)); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2729 | } |
| 2730 | break; |
| 2731 | case 'i': |
bellard | 92a31b1 | 2005-02-10 22:00:52 +0000 | [diff] [blame] | 2732 | case 'l': |
Luiz Capitulino | b6e098d | 2009-12-18 13:25:04 -0200 | [diff] [blame] | 2733 | case 'M': |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2734 | { |
blueswir1 | c2efc95 | 2007-09-25 17:28:42 +0000 | [diff] [blame] | 2735 | int64_t val; |
blueswir1 | 7743e58 | 2007-09-24 18:39:04 +0000 | [diff] [blame] | 2736 | |
blueswir1 | cd39008 | 2008-11-16 13:53:32 +0000 | [diff] [blame] | 2737 | while (qemu_isspace(*p)) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2738 | p++; |
bellard | 3440557 | 2004-06-08 00:55:58 +0000 | [diff] [blame] | 2739 | if (*typestr == '?' || *typestr == '.') { |
bellard | 3440557 | 2004-06-08 00:55:58 +0000 | [diff] [blame] | 2740 | if (*typestr == '?') { |
Luiz Capitulino | 5377358 | 2009-08-28 15:27:25 -0300 | [diff] [blame] | 2741 | if (*p == '\0') { |
| 2742 | typestr++; |
| 2743 | break; |
| 2744 | } |
bellard | 3440557 | 2004-06-08 00:55:58 +0000 | [diff] [blame] | 2745 | } else { |
| 2746 | if (*p == '.') { |
| 2747 | p++; |
blueswir1 | cd39008 | 2008-11-16 13:53:32 +0000 | [diff] [blame] | 2748 | while (qemu_isspace(*p)) |
bellard | 3440557 | 2004-06-08 00:55:58 +0000 | [diff] [blame] | 2749 | p++; |
bellard | 3440557 | 2004-06-08 00:55:58 +0000 | [diff] [blame] | 2750 | } else { |
Luiz Capitulino | 5377358 | 2009-08-28 15:27:25 -0300 | [diff] [blame] | 2751 | typestr++; |
| 2752 | break; |
bellard | 3440557 | 2004-06-08 00:55:58 +0000 | [diff] [blame] | 2753 | } |
| 2754 | } |
bellard | 13224a8 | 2006-07-14 22:03:35 +0000 | [diff] [blame] | 2755 | typestr++; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2756 | } |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2757 | if (get_expr(mon, &val, &p)) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2758 | goto fail; |
Luiz Capitulino | 675ebef | 2009-08-28 15:27:26 -0300 | [diff] [blame] | 2759 | /* Check if 'i' is greater than 32-bit */ |
| 2760 | if ((c == 'i') && ((val >> 32) & 0xffffffff)) { |
Bandan Das | ae50212 | 2015-06-03 18:38:08 -0400 | [diff] [blame] | 2761 | monitor_printf(mon, "\'%s\' has failed: ", cmd->name); |
Luiz Capitulino | 675ebef | 2009-08-28 15:27:26 -0300 | [diff] [blame] | 2762 | monitor_printf(mon, "integer is for 32-bit values\n"); |
| 2763 | goto fail; |
Luiz Capitulino | b6e098d | 2009-12-18 13:25:04 -0200 | [diff] [blame] | 2764 | } else if (c == 'M') { |
Luiz Capitulino | 9116284 | 2012-04-26 17:34:30 -0300 | [diff] [blame] | 2765 | if (val < 0) { |
| 2766 | monitor_printf(mon, "enter a positive value\n"); |
| 2767 | goto fail; |
| 2768 | } |
Luiz Capitulino | b6e098d | 2009-12-18 13:25:04 -0200 | [diff] [blame] | 2769 | val <<= 20; |
Luiz Capitulino | 675ebef | 2009-08-28 15:27:26 -0300 | [diff] [blame] | 2770 | } |
Luiz Capitulino | 5377358 | 2009-08-28 15:27:25 -0300 | [diff] [blame] | 2771 | qdict_put(qdict, key, qint_from_int(val)); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2772 | } |
| 2773 | break; |
Jes Sorensen | dbc0c67 | 2010-10-21 17:15:47 +0200 | [diff] [blame] | 2774 | case 'o': |
| 2775 | { |
Jes Sorensen | 70b4f4b | 2011-01-05 11:41:02 +0100 | [diff] [blame] | 2776 | int64_t val; |
Jes Sorensen | dbc0c67 | 2010-10-21 17:15:47 +0200 | [diff] [blame] | 2777 | char *end; |
| 2778 | |
| 2779 | while (qemu_isspace(*p)) { |
| 2780 | p++; |
| 2781 | } |
| 2782 | if (*typestr == '?') { |
| 2783 | typestr++; |
| 2784 | if (*p == '\0') { |
| 2785 | break; |
| 2786 | } |
| 2787 | } |
Marc-André Lureau | 4677bb4 | 2015-09-16 18:02:56 +0200 | [diff] [blame] | 2788 | val = qemu_strtosz(p, &end); |
Jes Sorensen | dbc0c67 | 2010-10-21 17:15:47 +0200 | [diff] [blame] | 2789 | if (val < 0) { |
| 2790 | monitor_printf(mon, "invalid size\n"); |
| 2791 | goto fail; |
| 2792 | } |
| 2793 | qdict_put(qdict, key, qint_from_int(val)); |
| 2794 | p = end; |
| 2795 | } |
| 2796 | break; |
Markus Armbruster | fccfb11e | 2010-01-25 14:23:06 +0100 | [diff] [blame] | 2797 | case 'T': |
Markus Armbruster | 3350a4d | 2010-01-25 14:23:03 +0100 | [diff] [blame] | 2798 | { |
| 2799 | double val; |
| 2800 | |
| 2801 | while (qemu_isspace(*p)) |
| 2802 | p++; |
| 2803 | if (*typestr == '?') { |
| 2804 | typestr++; |
| 2805 | if (*p == '\0') { |
| 2806 | break; |
| 2807 | } |
| 2808 | } |
| 2809 | if (get_double(mon, &val, &p) < 0) { |
| 2810 | goto fail; |
| 2811 | } |
Jes Sorensen | 07de3e6 | 2010-10-21 17:15:49 +0200 | [diff] [blame] | 2812 | if (p[0] && p[1] == 's') { |
Markus Armbruster | fccfb11e | 2010-01-25 14:23:06 +0100 | [diff] [blame] | 2813 | switch (*p) { |
| 2814 | case 'm': |
| 2815 | val /= 1e3; p += 2; break; |
| 2816 | case 'u': |
| 2817 | val /= 1e6; p += 2; break; |
| 2818 | case 'n': |
| 2819 | val /= 1e9; p += 2; break; |
| 2820 | } |
| 2821 | } |
Markus Armbruster | 3350a4d | 2010-01-25 14:23:03 +0100 | [diff] [blame] | 2822 | if (*p && !qemu_isspace(*p)) { |
| 2823 | monitor_printf(mon, "Unknown unit suffix\n"); |
| 2824 | goto fail; |
| 2825 | } |
| 2826 | qdict_put(qdict, key, qfloat_from_double(val)); |
| 2827 | } |
| 2828 | break; |
Markus Armbruster | 942cd1f | 2010-03-26 09:07:09 +0100 | [diff] [blame] | 2829 | case 'b': |
| 2830 | { |
| 2831 | const char *beg; |
Eric Blake | fc48ffc | 2015-05-15 16:24:59 -0600 | [diff] [blame] | 2832 | bool val; |
Markus Armbruster | 942cd1f | 2010-03-26 09:07:09 +0100 | [diff] [blame] | 2833 | |
| 2834 | while (qemu_isspace(*p)) { |
| 2835 | p++; |
| 2836 | } |
| 2837 | beg = p; |
| 2838 | while (qemu_isgraph(*p)) { |
| 2839 | p++; |
| 2840 | } |
| 2841 | if (p - beg == 2 && !memcmp(beg, "on", p - beg)) { |
Eric Blake | fc48ffc | 2015-05-15 16:24:59 -0600 | [diff] [blame] | 2842 | val = true; |
Markus Armbruster | 942cd1f | 2010-03-26 09:07:09 +0100 | [diff] [blame] | 2843 | } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) { |
Eric Blake | fc48ffc | 2015-05-15 16:24:59 -0600 | [diff] [blame] | 2844 | val = false; |
Markus Armbruster | 942cd1f | 2010-03-26 09:07:09 +0100 | [diff] [blame] | 2845 | } else { |
| 2846 | monitor_printf(mon, "Expected 'on' or 'off'\n"); |
| 2847 | goto fail; |
| 2848 | } |
Eric Blake | fc48ffc | 2015-05-15 16:24:59 -0600 | [diff] [blame] | 2849 | qdict_put(qdict, key, qbool_from_bool(val)); |
Markus Armbruster | 942cd1f | 2010-03-26 09:07:09 +0100 | [diff] [blame] | 2850 | } |
| 2851 | break; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2852 | case '-': |
| 2853 | { |
lirans@il.ibm.com | fbc3d96 | 2009-11-02 15:41:13 +0200 | [diff] [blame] | 2854 | const char *tmp = p; |
Luiz Capitulino | eb159d1 | 2010-05-28 15:25:24 -0300 | [diff] [blame] | 2855 | int skip_key = 0; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2856 | /* option */ |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2857 | |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2858 | c = *typestr++; |
| 2859 | if (c == '\0') |
| 2860 | goto bad_type; |
blueswir1 | cd39008 | 2008-11-16 13:53:32 +0000 | [diff] [blame] | 2861 | while (qemu_isspace(*p)) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2862 | p++; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2863 | if (*p == '-') { |
| 2864 | p++; |
lirans@il.ibm.com | fbc3d96 | 2009-11-02 15:41:13 +0200 | [diff] [blame] | 2865 | if(c != *p) { |
| 2866 | if(!is_valid_option(p, typestr)) { |
| 2867 | |
| 2868 | monitor_printf(mon, "%s: unsupported option -%c\n", |
Bandan Das | ae50212 | 2015-06-03 18:38:08 -0400 | [diff] [blame] | 2869 | cmd->name, *p); |
lirans@il.ibm.com | fbc3d96 | 2009-11-02 15:41:13 +0200 | [diff] [blame] | 2870 | goto fail; |
| 2871 | } else { |
| 2872 | skip_key = 1; |
| 2873 | } |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2874 | } |
lirans@il.ibm.com | fbc3d96 | 2009-11-02 15:41:13 +0200 | [diff] [blame] | 2875 | if(skip_key) { |
| 2876 | p = tmp; |
| 2877 | } else { |
Luiz Capitulino | eb159d1 | 2010-05-28 15:25:24 -0300 | [diff] [blame] | 2878 | /* has option */ |
lirans@il.ibm.com | fbc3d96 | 2009-11-02 15:41:13 +0200 | [diff] [blame] | 2879 | p++; |
Eric Blake | fc48ffc | 2015-05-15 16:24:59 -0600 | [diff] [blame] | 2880 | qdict_put(qdict, key, qbool_from_bool(true)); |
lirans@il.ibm.com | fbc3d96 | 2009-11-02 15:41:13 +0200 | [diff] [blame] | 2881 | } |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2882 | } |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2883 | } |
| 2884 | break; |
Wenchao Xia | 129be00 | 2013-08-27 20:38:26 +0800 | [diff] [blame] | 2885 | case 'S': |
| 2886 | { |
| 2887 | /* package all remaining string */ |
| 2888 | int len; |
| 2889 | |
| 2890 | while (qemu_isspace(*p)) { |
| 2891 | p++; |
| 2892 | } |
| 2893 | if (*typestr == '?') { |
| 2894 | typestr++; |
| 2895 | if (*p == '\0') { |
| 2896 | /* no remaining string: NULL argument */ |
| 2897 | break; |
| 2898 | } |
| 2899 | } |
| 2900 | len = strlen(p); |
| 2901 | if (len <= 0) { |
| 2902 | monitor_printf(mon, "%s: string expected\n", |
Bandan Das | ae50212 | 2015-06-03 18:38:08 -0400 | [diff] [blame] | 2903 | cmd->name); |
Bandan Das | e549d2a | 2015-06-03 18:38:10 -0400 | [diff] [blame] | 2904 | goto fail; |
Wenchao Xia | 129be00 | 2013-08-27 20:38:26 +0800 | [diff] [blame] | 2905 | } |
| 2906 | qdict_put(qdict, key, qstring_from_str(p)); |
| 2907 | p += len; |
| 2908 | } |
| 2909 | break; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2910 | default: |
| 2911 | bad_type: |
Bandan Das | ae50212 | 2015-06-03 18:38:08 -0400 | [diff] [blame] | 2912 | monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2913 | goto fail; |
| 2914 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2915 | g_free(key); |
Luiz Capitulino | 4d76d2b | 2009-08-28 15:27:09 -0300 | [diff] [blame] | 2916 | key = NULL; |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2917 | } |
| 2918 | /* check that all arguments were parsed */ |
blueswir1 | cd39008 | 2008-11-16 13:53:32 +0000 | [diff] [blame] | 2919 | while (qemu_isspace(*p)) |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2920 | p++; |
| 2921 | if (*p != '\0') { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2922 | monitor_printf(mon, "%s: extraneous characters at the end of line\n", |
Bandan Das | ae50212 | 2015-06-03 18:38:08 -0400 | [diff] [blame] | 2923 | cmd->name); |
bellard | 9307c4c | 2004-04-04 12:57:25 +0000 | [diff] [blame] | 2924 | goto fail; |
| 2925 | } |
| 2926 | |
Bandan Das | ae50212 | 2015-06-03 18:38:08 -0400 | [diff] [blame] | 2927 | return qdict; |
Gerd Hoffmann | ac7531e | 2009-08-14 10:36:06 +0200 | [diff] [blame] | 2928 | |
Luiz Capitulino | 55f81d9 | 2009-08-28 15:27:22 -0300 | [diff] [blame] | 2929 | fail: |
Bandan Das | ae50212 | 2015-06-03 18:38:08 -0400 | [diff] [blame] | 2930 | QDECREF(qdict); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2931 | g_free(key); |
Luiz Capitulino | 55f81d9 | 2009-08-28 15:27:22 -0300 | [diff] [blame] | 2932 | return NULL; |
| 2933 | } |
| 2934 | |
Markus Armbruster | 7ef6cf6 | 2015-03-06 19:12:36 +0100 | [diff] [blame] | 2935 | static void handle_hmp_command(Monitor *mon, const char *cmdline) |
Luiz Capitulino | 55f81d9 | 2009-08-28 15:27:22 -0300 | [diff] [blame] | 2936 | { |
Luiz Capitulino | 55f81d9 | 2009-08-28 15:27:22 -0300 | [diff] [blame] | 2937 | QDict *qdict; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2938 | const mon_cmd_t *cmd; |
Luiz Capitulino | 55f81d9 | 2009-08-28 15:27:22 -0300 | [diff] [blame] | 2939 | |
Bandan Das | ae50212 | 2015-06-03 18:38:08 -0400 | [diff] [blame] | 2940 | cmd = monitor_parse_command(mon, &cmdline, mon->cmd_table); |
| 2941 | if (!cmd) { |
| 2942 | return; |
Luiz Capitulino | 55f81d9 | 2009-08-28 15:27:22 -0300 | [diff] [blame] | 2943 | } |
| 2944 | |
Bandan Das | ae50212 | 2015-06-03 18:38:08 -0400 | [diff] [blame] | 2945 | qdict = monitor_parse_arguments(mon, &cmdline, cmd); |
| 2946 | if (!qdict) { |
Bandan Das | dd41eea | 2015-06-03 18:38:09 -0400 | [diff] [blame] | 2947 | monitor_printf(mon, "Try \"help %s\" for more information\n", |
| 2948 | cmd->name); |
Bandan Das | ae50212 | 2015-06-03 18:38:08 -0400 | [diff] [blame] | 2949 | return; |
| 2950 | } |
| 2951 | |
Marc-André Lureau | 2b9e357 | 2016-09-12 13:19:06 +0400 | [diff] [blame] | 2952 | cmd->cmd(mon, qdict); |
Luiz Capitulino | f7188bb | 2009-08-28 15:27:10 -0300 | [diff] [blame] | 2953 | QDECREF(qdict); |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 2954 | } |
| 2955 | |
Wenchao Xia | cd5c6bb | 2013-08-27 20:38:13 +0800 | [diff] [blame] | 2956 | static void cmd_completion(Monitor *mon, const char *name, const char *list) |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 2957 | { |
| 2958 | const char *p, *pstart; |
| 2959 | char cmd[128]; |
| 2960 | int len; |
| 2961 | |
| 2962 | p = list; |
| 2963 | for(;;) { |
| 2964 | pstart = p; |
| 2965 | p = strchr(p, '|'); |
| 2966 | if (!p) |
| 2967 | p = pstart + strlen(pstart); |
| 2968 | len = p - pstart; |
| 2969 | if (len > sizeof(cmd) - 2) |
| 2970 | len = sizeof(cmd) - 2; |
| 2971 | memcpy(cmd, pstart, len); |
| 2972 | cmd[len] = '\0'; |
| 2973 | if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) { |
Wenchao Xia | cd5c6bb | 2013-08-27 20:38:13 +0800 | [diff] [blame] | 2974 | readline_add_completion(mon->rs, cmd); |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 2975 | } |
| 2976 | if (*p == '\0') |
| 2977 | break; |
| 2978 | p++; |
| 2979 | } |
| 2980 | } |
| 2981 | |
Wenchao Xia | cb8f68b | 2013-08-27 20:38:14 +0800 | [diff] [blame] | 2982 | static void file_completion(Monitor *mon, const char *input) |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 2983 | { |
| 2984 | DIR *ffs; |
| 2985 | struct dirent *d; |
| 2986 | char path[1024]; |
| 2987 | char file[1024], file_prefix[1024]; |
| 2988 | int input_path_len; |
| 2989 | const char *p; |
| 2990 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2991 | p = strrchr(input, '/'); |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 2992 | if (!p) { |
| 2993 | input_path_len = 0; |
| 2994 | pstrcpy(file_prefix, sizeof(file_prefix), input); |
blueswir1 | 363a37d | 2008-08-21 17:58:08 +0000 | [diff] [blame] | 2995 | pstrcpy(path, sizeof(path), "."); |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 2996 | } else { |
| 2997 | input_path_len = p - input + 1; |
| 2998 | memcpy(path, input, input_path_len); |
| 2999 | if (input_path_len > sizeof(path) - 1) |
| 3000 | input_path_len = sizeof(path) - 1; |
| 3001 | path[input_path_len] = '\0'; |
| 3002 | pstrcpy(file_prefix, sizeof(file_prefix), p + 1); |
| 3003 | } |
Bandan Das | 19f2db5 | 2015-06-03 18:38:07 -0400 | [diff] [blame] | 3004 | |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 3005 | ffs = opendir(path); |
| 3006 | if (!ffs) |
| 3007 | return; |
| 3008 | for(;;) { |
| 3009 | struct stat sb; |
| 3010 | d = readdir(ffs); |
| 3011 | if (!d) |
| 3012 | break; |
Kusanagi Kouichi | 46c7fc1 | 2010-10-20 18:00:01 +0900 | [diff] [blame] | 3013 | |
| 3014 | if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) { |
| 3015 | continue; |
| 3016 | } |
| 3017 | |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 3018 | if (strstart(d->d_name, file_prefix, NULL)) { |
| 3019 | memcpy(file, input, input_path_len); |
blueswir1 | 363a37d | 2008-08-21 17:58:08 +0000 | [diff] [blame] | 3020 | if (input_path_len < sizeof(file)) |
| 3021 | pstrcpy(file + input_path_len, sizeof(file) - input_path_len, |
| 3022 | d->d_name); |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 3023 | /* stat the file to find out if it's a directory. |
| 3024 | * In that case add a slash to speed up typing long paths |
| 3025 | */ |
Markus Armbruster | c951d9a | 2011-11-16 15:43:47 +0100 | [diff] [blame] | 3026 | if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) { |
blueswir1 | 363a37d | 2008-08-21 17:58:08 +0000 | [diff] [blame] | 3027 | pstrcat(file, sizeof(file), "/"); |
Markus Armbruster | c951d9a | 2011-11-16 15:43:47 +0100 | [diff] [blame] | 3028 | } |
Wenchao Xia | cb8f68b | 2013-08-27 20:38:14 +0800 | [diff] [blame] | 3029 | readline_add_completion(mon->rs, file); |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 3030 | } |
| 3031 | } |
| 3032 | closedir(ffs); |
| 3033 | } |
| 3034 | |
Luiz Capitulino | 4d76d2b | 2009-08-28 15:27:09 -0300 | [diff] [blame] | 3035 | static const char *next_arg_type(const char *typestr) |
| 3036 | { |
| 3037 | const char *p = strchr(typestr, ':'); |
| 3038 | return (p != NULL ? ++p : typestr); |
| 3039 | } |
| 3040 | |
Hani Benhabiles | 40d1939 | 2014-05-07 23:41:30 +0100 | [diff] [blame] | 3041 | static void add_completion_option(ReadLineState *rs, const char *str, |
| 3042 | const char *option) |
| 3043 | { |
| 3044 | if (!str || !option) { |
| 3045 | return; |
| 3046 | } |
| 3047 | if (!strncmp(option, str, strlen(str))) { |
| 3048 | readline_add_completion(rs, option); |
| 3049 | } |
| 3050 | } |
| 3051 | |
Hani Benhabiles | 13e315d | 2014-05-07 23:41:29 +0100 | [diff] [blame] | 3052 | void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str) |
| 3053 | { |
| 3054 | size_t len; |
| 3055 | ChardevBackendInfoList *list, *start; |
| 3056 | |
| 3057 | if (nb_args != 2) { |
| 3058 | return; |
| 3059 | } |
| 3060 | len = strlen(str); |
| 3061 | readline_set_completion_index(rs, len); |
| 3062 | |
| 3063 | start = list = qmp_query_chardev_backends(NULL); |
| 3064 | while (list) { |
| 3065 | const char *chr_name = list->value->name; |
| 3066 | |
| 3067 | if (!strncmp(chr_name, str, len)) { |
| 3068 | readline_add_completion(rs, chr_name); |
| 3069 | } |
| 3070 | list = list->next; |
| 3071 | } |
| 3072 | qapi_free_ChardevBackendInfoList(start); |
| 3073 | } |
| 3074 | |
Hani Benhabiles | b162b49 | 2014-05-07 23:41:31 +0100 | [diff] [blame] | 3075 | void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str) |
| 3076 | { |
| 3077 | size_t len; |
| 3078 | int i; |
| 3079 | |
| 3080 | if (nb_args != 2) { |
| 3081 | return; |
| 3082 | } |
| 3083 | len = strlen(str); |
| 3084 | readline_set_completion_index(rs, len); |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 3085 | for (i = 0; NetClientDriver_lookup[i]; i++) { |
| 3086 | add_completion_option(rs, str, NetClientDriver_lookup[i]); |
Hani Benhabiles | b162b49 | 2014-05-07 23:41:31 +0100 | [diff] [blame] | 3087 | } |
| 3088 | } |
| 3089 | |
Hani Benhabiles | 2da1b3a | 2014-04-13 16:25:07 +0100 | [diff] [blame] | 3090 | void device_add_completion(ReadLineState *rs, int nb_args, const char *str) |
Hani Benhabiles | 992d3e6 | 2014-02-06 23:30:11 +0100 | [diff] [blame] | 3091 | { |
| 3092 | GSList *list, *elt; |
| 3093 | size_t len; |
| 3094 | |
Hani Benhabiles | 2da1b3a | 2014-04-13 16:25:07 +0100 | [diff] [blame] | 3095 | if (nb_args != 2) { |
| 3096 | return; |
| 3097 | } |
| 3098 | |
Hani Benhabiles | 992d3e6 | 2014-02-06 23:30:11 +0100 | [diff] [blame] | 3099 | len = strlen(str); |
| 3100 | readline_set_completion_index(rs, len); |
| 3101 | list = elt = object_class_get_list(TYPE_DEVICE, false); |
| 3102 | while (elt) { |
| 3103 | const char *name; |
| 3104 | DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data, |
| 3105 | TYPE_DEVICE); |
| 3106 | name = object_class_get_name(OBJECT_CLASS(dc)); |
Hani Benhabiles | 2da1b3a | 2014-04-13 16:25:07 +0100 | [diff] [blame] | 3107 | |
| 3108 | if (!dc->cannot_instantiate_with_device_add_yet |
| 3109 | && !strncmp(name, str, len)) { |
Hani Benhabiles | 992d3e6 | 2014-02-06 23:30:11 +0100 | [diff] [blame] | 3110 | readline_add_completion(rs, name); |
| 3111 | } |
| 3112 | elt = elt->next; |
| 3113 | } |
| 3114 | g_slist_free(list); |
| 3115 | } |
| 3116 | |
Hani Benhabiles | bfa40f7 | 2014-04-13 16:25:06 +0100 | [diff] [blame] | 3117 | void object_add_completion(ReadLineState *rs, int nb_args, const char *str) |
Hani Benhabiles | 1094fd3 | 2014-02-06 23:30:13 +0100 | [diff] [blame] | 3118 | { |
| 3119 | GSList *list, *elt; |
| 3120 | size_t len; |
| 3121 | |
Hani Benhabiles | bfa40f7 | 2014-04-13 16:25:06 +0100 | [diff] [blame] | 3122 | if (nb_args != 2) { |
| 3123 | return; |
| 3124 | } |
| 3125 | |
Hani Benhabiles | 1094fd3 | 2014-02-06 23:30:13 +0100 | [diff] [blame] | 3126 | len = strlen(str); |
| 3127 | readline_set_completion_index(rs, len); |
| 3128 | list = elt = object_class_get_list(TYPE_USER_CREATABLE, false); |
| 3129 | while (elt) { |
| 3130 | const char *name; |
| 3131 | |
| 3132 | name = object_class_get_name(OBJECT_CLASS(elt->data)); |
| 3133 | if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) { |
| 3134 | readline_add_completion(rs, name); |
| 3135 | } |
| 3136 | elt = elt->next; |
| 3137 | } |
| 3138 | g_slist_free(list); |
| 3139 | } |
| 3140 | |
Zhu Guihua | 6a1fa9f | 2014-10-21 19:46:05 +0800 | [diff] [blame] | 3141 | static void peripheral_device_del_completion(ReadLineState *rs, |
| 3142 | const char *str, size_t len) |
| 3143 | { |
Marcel Apfelbaum | 4cae4d5 | 2014-11-26 13:50:01 +0200 | [diff] [blame] | 3144 | Object *peripheral = container_get(qdev_get_machine(), "/peripheral"); |
| 3145 | GSList *list, *item; |
Zhu Guihua | 6a1fa9f | 2014-10-21 19:46:05 +0800 | [diff] [blame] | 3146 | |
Marcel Apfelbaum | 4cae4d5 | 2014-11-26 13:50:01 +0200 | [diff] [blame] | 3147 | list = qdev_build_hotpluggable_device_list(peripheral); |
| 3148 | if (!list) { |
Zhu Guihua | 6a1fa9f | 2014-10-21 19:46:05 +0800 | [diff] [blame] | 3149 | return; |
| 3150 | } |
| 3151 | |
Zhu Guihua | 6a1fa9f | 2014-10-21 19:46:05 +0800 | [diff] [blame] | 3152 | for (item = list; item; item = g_slist_next(item)) { |
| 3153 | DeviceState *dev = item->data; |
| 3154 | |
| 3155 | if (dev->id && !strncmp(str, dev->id, len)) { |
| 3156 | readline_add_completion(rs, dev->id); |
| 3157 | } |
| 3158 | } |
| 3159 | |
| 3160 | g_slist_free(list); |
| 3161 | } |
| 3162 | |
Hani Benhabiles | 6297d9a | 2014-05-07 23:41:28 +0100 | [diff] [blame] | 3163 | void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str) |
| 3164 | { |
| 3165 | size_t len; |
| 3166 | ChardevInfoList *list, *start; |
| 3167 | |
| 3168 | if (nb_args != 2) { |
| 3169 | return; |
| 3170 | } |
| 3171 | len = strlen(str); |
| 3172 | readline_set_completion_index(rs, len); |
| 3173 | |
| 3174 | start = list = qmp_query_chardev(NULL); |
| 3175 | while (list) { |
| 3176 | ChardevInfo *chr = list->value; |
| 3177 | |
| 3178 | if (!strncmp(chr->label, str, len)) { |
| 3179 | readline_add_completion(rs, chr->label); |
| 3180 | } |
| 3181 | list = list->next; |
| 3182 | } |
| 3183 | qapi_free_ChardevInfoList(start); |
| 3184 | } |
| 3185 | |
Hani Benhabiles | 8e59777 | 2014-05-27 23:39:30 +0100 | [diff] [blame] | 3186 | static void ringbuf_completion(ReadLineState *rs, const char *str) |
| 3187 | { |
| 3188 | size_t len; |
| 3189 | ChardevInfoList *list, *start; |
| 3190 | |
| 3191 | len = strlen(str); |
| 3192 | readline_set_completion_index(rs, len); |
| 3193 | |
| 3194 | start = list = qmp_query_chardev(NULL); |
| 3195 | while (list) { |
| 3196 | ChardevInfo *chr_info = list->value; |
| 3197 | |
| 3198 | if (!strncmp(chr_info->label, str, len)) { |
| 3199 | CharDriverState *chr = qemu_chr_find(chr_info->label); |
| 3200 | if (chr && chr_is_ringbuf(chr)) { |
| 3201 | readline_add_completion(rs, chr_info->label); |
| 3202 | } |
| 3203 | } |
| 3204 | list = list->next; |
| 3205 | } |
| 3206 | qapi_free_ChardevInfoList(start); |
| 3207 | } |
| 3208 | |
Hani Benhabiles | 8e59777 | 2014-05-27 23:39:30 +0100 | [diff] [blame] | 3209 | void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str) |
| 3210 | { |
| 3211 | if (nb_args != 2) { |
| 3212 | return; |
| 3213 | } |
| 3214 | ringbuf_completion(rs, str); |
| 3215 | } |
| 3216 | |
Hani Benhabiles | 2da1b3a | 2014-04-13 16:25:07 +0100 | [diff] [blame] | 3217 | void device_del_completion(ReadLineState *rs, int nb_args, const char *str) |
| 3218 | { |
| 3219 | size_t len; |
| 3220 | |
| 3221 | if (nb_args != 2) { |
| 3222 | return; |
| 3223 | } |
| 3224 | |
| 3225 | len = strlen(str); |
| 3226 | readline_set_completion_index(rs, len); |
Zhu Guihua | 6a1fa9f | 2014-10-21 19:46:05 +0800 | [diff] [blame] | 3227 | peripheral_device_del_completion(rs, str, len); |
Hani Benhabiles | 2da1b3a | 2014-04-13 16:25:07 +0100 | [diff] [blame] | 3228 | } |
| 3229 | |
Hani Benhabiles | bfa40f7 | 2014-04-13 16:25:06 +0100 | [diff] [blame] | 3230 | void object_del_completion(ReadLineState *rs, int nb_args, const char *str) |
Hani Benhabiles | b48fa07 | 2014-02-06 23:30:12 +0100 | [diff] [blame] | 3231 | { |
| 3232 | ObjectPropertyInfoList *list, *start; |
| 3233 | size_t len; |
| 3234 | |
Hani Benhabiles | bfa40f7 | 2014-04-13 16:25:06 +0100 | [diff] [blame] | 3235 | if (nb_args != 2) { |
| 3236 | return; |
| 3237 | } |
Hani Benhabiles | b48fa07 | 2014-02-06 23:30:12 +0100 | [diff] [blame] | 3238 | len = strlen(str); |
| 3239 | readline_set_completion_index(rs, len); |
| 3240 | |
| 3241 | start = list = qmp_qom_list("/objects", NULL); |
| 3242 | while (list) { |
| 3243 | ObjectPropertyInfo *info = list->value; |
| 3244 | |
| 3245 | if (!strncmp(info->type, "child<", 5) |
| 3246 | && !strncmp(info->name, str, len)) { |
| 3247 | readline_add_completion(rs, info->name); |
| 3248 | } |
| 3249 | list = list->next; |
| 3250 | } |
| 3251 | qapi_free_ObjectPropertyInfoList(start); |
| 3252 | } |
| 3253 | |
Hani Benhabiles | 29136cd | 2014-05-07 23:41:27 +0100 | [diff] [blame] | 3254 | void sendkey_completion(ReadLineState *rs, int nb_args, const char *str) |
| 3255 | { |
| 3256 | int i; |
| 3257 | char *sep; |
| 3258 | size_t len; |
| 3259 | |
| 3260 | if (nb_args != 2) { |
| 3261 | return; |
| 3262 | } |
| 3263 | sep = strrchr(str, '-'); |
| 3264 | if (sep) { |
| 3265 | str = sep + 1; |
| 3266 | } |
| 3267 | len = strlen(str); |
| 3268 | readline_set_completion_index(rs, len); |
Eric Blake | 7fb1cf1 | 2015-11-18 01:52:57 -0700 | [diff] [blame] | 3269 | for (i = 0; i < Q_KEY_CODE__MAX; i++) { |
Hani Benhabiles | 29136cd | 2014-05-07 23:41:27 +0100 | [diff] [blame] | 3270 | if (!strncmp(str, QKeyCode_lookup[i], len)) { |
| 3271 | readline_add_completion(rs, QKeyCode_lookup[i]); |
| 3272 | } |
| 3273 | } |
| 3274 | } |
| 3275 | |
Hani Benhabiles | 40d1939 | 2014-05-07 23:41:30 +0100 | [diff] [blame] | 3276 | void set_link_completion(ReadLineState *rs, int nb_args, const char *str) |
| 3277 | { |
| 3278 | size_t len; |
| 3279 | |
| 3280 | len = strlen(str); |
| 3281 | readline_set_completion_index(rs, len); |
| 3282 | if (nb_args == 2) { |
Jason Wang | eaed483 | 2015-04-23 14:21:38 +0800 | [diff] [blame] | 3283 | NetClientState *ncs[MAX_QUEUE_NUM]; |
Hani Benhabiles | 40d1939 | 2014-05-07 23:41:30 +0100 | [diff] [blame] | 3284 | int count, i; |
| 3285 | count = qemu_find_net_clients_except(NULL, ncs, |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 3286 | NET_CLIENT_DRIVER_NONE, |
Jason Wang | eaed483 | 2015-04-23 14:21:38 +0800 | [diff] [blame] | 3287 | MAX_QUEUE_NUM); |
Jason Wang | bcfa4d6 | 2015-04-23 14:21:39 +0800 | [diff] [blame] | 3288 | for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) { |
Hani Benhabiles | 40d1939 | 2014-05-07 23:41:30 +0100 | [diff] [blame] | 3289 | const char *name = ncs[i]->name; |
| 3290 | if (!strncmp(str, name, len)) { |
| 3291 | readline_add_completion(rs, name); |
| 3292 | } |
| 3293 | } |
| 3294 | } else if (nb_args == 3) { |
| 3295 | add_completion_option(rs, str, "on"); |
| 3296 | add_completion_option(rs, str, "off"); |
| 3297 | } |
| 3298 | } |
| 3299 | |
Hani Benhabiles | 11b389f | 2014-05-07 23:41:32 +0100 | [diff] [blame] | 3300 | void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str) |
| 3301 | { |
| 3302 | int len, count, i; |
Jason Wang | eaed483 | 2015-04-23 14:21:38 +0800 | [diff] [blame] | 3303 | NetClientState *ncs[MAX_QUEUE_NUM]; |
Hani Benhabiles | 11b389f | 2014-05-07 23:41:32 +0100 | [diff] [blame] | 3304 | |
| 3305 | if (nb_args != 2) { |
| 3306 | return; |
| 3307 | } |
| 3308 | |
| 3309 | len = strlen(str); |
| 3310 | readline_set_completion_index(rs, len); |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 3311 | count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC, |
Jason Wang | eaed483 | 2015-04-23 14:21:38 +0800 | [diff] [blame] | 3312 | MAX_QUEUE_NUM); |
Jason Wang | bcfa4d6 | 2015-04-23 14:21:39 +0800 | [diff] [blame] | 3313 | for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) { |
Hani Benhabiles | 11b389f | 2014-05-07 23:41:32 +0100 | [diff] [blame] | 3314 | QemuOpts *opts; |
| 3315 | const char *name = ncs[i]->name; |
| 3316 | if (strncmp(str, name, len)) { |
| 3317 | continue; |
| 3318 | } |
| 3319 | opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name); |
| 3320 | if (opts) { |
| 3321 | readline_add_completion(rs, name); |
| 3322 | } |
| 3323 | } |
| 3324 | } |
| 3325 | |
Lluís Vilanova | bd71211 | 2016-07-11 12:53:51 +0200 | [diff] [blame] | 3326 | void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str) |
| 3327 | { |
| 3328 | size_t len; |
| 3329 | |
| 3330 | len = strlen(str); |
| 3331 | readline_set_completion_index(rs, len); |
| 3332 | if (nb_args == 2) { |
| 3333 | TraceEventID id; |
| 3334 | for (id = 0; id < trace_event_count(); id++) { |
| 3335 | const char *event_name = trace_event_get_name(trace_event_id(id)); |
| 3336 | if (!strncmp(str, event_name, len)) { |
| 3337 | readline_add_completion(rs, event_name); |
| 3338 | } |
| 3339 | } |
| 3340 | } |
| 3341 | } |
| 3342 | |
Dr. David Alan Gilbert | 987bd27 | 2015-08-14 11:27:43 +0100 | [diff] [blame] | 3343 | void trace_event_completion(ReadLineState *rs, int nb_args, const char *str) |
| 3344 | { |
| 3345 | size_t len; |
| 3346 | |
| 3347 | len = strlen(str); |
| 3348 | readline_set_completion_index(rs, len); |
| 3349 | if (nb_args == 2) { |
| 3350 | TraceEventID id; |
| 3351 | for (id = 0; id < trace_event_count(); id++) { |
| 3352 | const char *event_name = trace_event_get_name(trace_event_id(id)); |
| 3353 | if (!strncmp(str, event_name, len)) { |
| 3354 | readline_add_completion(rs, event_name); |
| 3355 | } |
| 3356 | } |
| 3357 | } else if (nb_args == 3) { |
| 3358 | add_completion_option(rs, str, "on"); |
| 3359 | add_completion_option(rs, str, "off"); |
| 3360 | } |
| 3361 | } |
| 3362 | |
Hani Benhabiles | d0ece34 | 2014-05-27 23:39:31 +0100 | [diff] [blame] | 3363 | void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str) |
| 3364 | { |
Hani Benhabiles | 4bb08af | 2014-07-29 23:22:40 +0100 | [diff] [blame] | 3365 | int i; |
| 3366 | |
Hani Benhabiles | d0ece34 | 2014-05-27 23:39:31 +0100 | [diff] [blame] | 3367 | if (nb_args != 2) { |
| 3368 | return; |
| 3369 | } |
| 3370 | readline_set_completion_index(rs, strlen(str)); |
Hani Benhabiles | 4bb08af | 2014-07-29 23:22:40 +0100 | [diff] [blame] | 3371 | for (i = 0; WatchdogExpirationAction_lookup[i]; i++) { |
| 3372 | add_completion_option(rs, str, WatchdogExpirationAction_lookup[i]); |
| 3373 | } |
Hani Benhabiles | d0ece34 | 2014-05-27 23:39:31 +0100 | [diff] [blame] | 3374 | } |
| 3375 | |
Hani Benhabiles | c68a040 | 2014-05-27 23:39:32 +0100 | [diff] [blame] | 3376 | void migrate_set_capability_completion(ReadLineState *rs, int nb_args, |
| 3377 | const char *str) |
| 3378 | { |
| 3379 | size_t len; |
| 3380 | |
| 3381 | len = strlen(str); |
| 3382 | readline_set_completion_index(rs, len); |
| 3383 | if (nb_args == 2) { |
| 3384 | int i; |
Eric Blake | 7fb1cf1 | 2015-11-18 01:52:57 -0700 | [diff] [blame] | 3385 | for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) { |
Hani Benhabiles | c68a040 | 2014-05-27 23:39:32 +0100 | [diff] [blame] | 3386 | const char *name = MigrationCapability_lookup[i]; |
| 3387 | if (!strncmp(str, name, len)) { |
| 3388 | readline_add_completion(rs, name); |
| 3389 | } |
| 3390 | } |
| 3391 | } else if (nb_args == 3) { |
| 3392 | add_completion_option(rs, str, "on"); |
| 3393 | add_completion_option(rs, str, "off"); |
| 3394 | } |
| 3395 | } |
| 3396 | |
Liang Li | 50e9a62 | 2015-03-23 16:32:29 +0800 | [diff] [blame] | 3397 | void migrate_set_parameter_completion(ReadLineState *rs, int nb_args, |
| 3398 | const char *str) |
| 3399 | { |
| 3400 | size_t len; |
| 3401 | |
| 3402 | len = strlen(str); |
| 3403 | readline_set_completion_index(rs, len); |
| 3404 | if (nb_args == 2) { |
| 3405 | int i; |
Eric Blake | 7fb1cf1 | 2015-11-18 01:52:57 -0700 | [diff] [blame] | 3406 | for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) { |
Liang Li | 50e9a62 | 2015-03-23 16:32:29 +0800 | [diff] [blame] | 3407 | const char *name = MigrationParameter_lookup[i]; |
| 3408 | if (!strncmp(str, name, len)) { |
| 3409 | readline_add_completion(rs, name); |
| 3410 | } |
| 3411 | } |
| 3412 | } |
| 3413 | } |
| 3414 | |
Hani Benhabiles | e3bb532 | 2014-05-27 23:39:34 +0100 | [diff] [blame] | 3415 | void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str) |
| 3416 | { |
| 3417 | int i; |
| 3418 | size_t len; |
| 3419 | if (nb_args != 2) { |
| 3420 | return; |
| 3421 | } |
| 3422 | len = strlen(str); |
| 3423 | readline_set_completion_index(rs, len); |
| 3424 | for (i = 0; host_net_devices[i]; i++) { |
| 3425 | if (!strncmp(host_net_devices[i], str, len)) { |
| 3426 | readline_add_completion(rs, host_net_devices[i]); |
| 3427 | } |
| 3428 | } |
| 3429 | } |
| 3430 | |
Hani Benhabiles | ddd6b45 | 2014-05-27 23:39:36 +0100 | [diff] [blame] | 3431 | void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str) |
| 3432 | { |
Jason Wang | eaed483 | 2015-04-23 14:21:38 +0800 | [diff] [blame] | 3433 | NetClientState *ncs[MAX_QUEUE_NUM]; |
Hani Benhabiles | ddd6b45 | 2014-05-27 23:39:36 +0100 | [diff] [blame] | 3434 | int count, i, len; |
| 3435 | |
| 3436 | len = strlen(str); |
| 3437 | readline_set_completion_index(rs, len); |
| 3438 | if (nb_args == 2) { |
| 3439 | count = qemu_find_net_clients_except(NULL, ncs, |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 3440 | NET_CLIENT_DRIVER_NONE, |
Jason Wang | eaed483 | 2015-04-23 14:21:38 +0800 | [diff] [blame] | 3441 | MAX_QUEUE_NUM); |
Jason Wang | bcfa4d6 | 2015-04-23 14:21:39 +0800 | [diff] [blame] | 3442 | for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) { |
Hani Benhabiles | ddd6b45 | 2014-05-27 23:39:36 +0100 | [diff] [blame] | 3443 | int id; |
| 3444 | char name[16]; |
| 3445 | |
| 3446 | if (net_hub_id_for_client(ncs[i], &id)) { |
| 3447 | continue; |
| 3448 | } |
| 3449 | snprintf(name, sizeof(name), "%d", id); |
| 3450 | if (!strncmp(str, name, len)) { |
| 3451 | readline_add_completion(rs, name); |
| 3452 | } |
| 3453 | } |
| 3454 | return; |
| 3455 | } else if (nb_args == 3) { |
| 3456 | count = qemu_find_net_clients_except(NULL, ncs, |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 3457 | NET_CLIENT_DRIVER_NIC, |
Jason Wang | eaed483 | 2015-04-23 14:21:38 +0800 | [diff] [blame] | 3458 | MAX_QUEUE_NUM); |
Jason Wang | bcfa4d6 | 2015-04-23 14:21:39 +0800 | [diff] [blame] | 3459 | for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) { |
Jason Wang | 2c4681f | 2015-02-02 15:06:38 +0800 | [diff] [blame] | 3460 | int id; |
Hani Benhabiles | ddd6b45 | 2014-05-27 23:39:36 +0100 | [diff] [blame] | 3461 | const char *name; |
| 3462 | |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 3463 | if (ncs[i]->info->type == NET_CLIENT_DRIVER_HUBPORT || |
Jason Wang | 2c4681f | 2015-02-02 15:06:38 +0800 | [diff] [blame] | 3464 | net_hub_id_for_client(ncs[i], &id)) { |
| 3465 | continue; |
| 3466 | } |
Hani Benhabiles | ddd6b45 | 2014-05-27 23:39:36 +0100 | [diff] [blame] | 3467 | name = ncs[i]->name; |
| 3468 | if (!strncmp(str, name, len)) { |
| 3469 | readline_add_completion(rs, name); |
| 3470 | } |
| 3471 | } |
| 3472 | return; |
| 3473 | } |
| 3474 | } |
| 3475 | |
Hani Benhabiles | b21631f | 2014-05-27 23:39:37 +0100 | [diff] [blame] | 3476 | static void vm_completion(ReadLineState *rs, const char *str) |
| 3477 | { |
| 3478 | size_t len; |
Kevin Wolf | 7c8eece | 2016-03-22 18:58:50 +0100 | [diff] [blame] | 3479 | BlockDriverState *bs; |
Kevin Wolf | 88be7b4 | 2016-05-20 18:49:07 +0200 | [diff] [blame] | 3480 | BdrvNextIterator it; |
Hani Benhabiles | b21631f | 2014-05-27 23:39:37 +0100 | [diff] [blame] | 3481 | |
| 3482 | len = strlen(str); |
| 3483 | readline_set_completion_index(rs, len); |
Kevin Wolf | 7c8eece | 2016-03-22 18:58:50 +0100 | [diff] [blame] | 3484 | |
Kevin Wolf | 88be7b4 | 2016-05-20 18:49:07 +0200 | [diff] [blame] | 3485 | for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) { |
Hani Benhabiles | b21631f | 2014-05-27 23:39:37 +0100 | [diff] [blame] | 3486 | SnapshotInfoList *snapshots, *snapshot; |
Denis V. Lunev | 6bf1faa | 2015-11-04 20:19:42 +0300 | [diff] [blame] | 3487 | AioContext *ctx = bdrv_get_aio_context(bs); |
| 3488 | bool ok = false; |
Hani Benhabiles | b21631f | 2014-05-27 23:39:37 +0100 | [diff] [blame] | 3489 | |
Denis V. Lunev | 6bf1faa | 2015-11-04 20:19:42 +0300 | [diff] [blame] | 3490 | aio_context_acquire(ctx); |
| 3491 | if (bdrv_can_snapshot(bs)) { |
| 3492 | ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0; |
| 3493 | } |
| 3494 | aio_context_release(ctx); |
| 3495 | if (!ok) { |
Hani Benhabiles | b21631f | 2014-05-27 23:39:37 +0100 | [diff] [blame] | 3496 | continue; |
| 3497 | } |
Denis V. Lunev | 6bf1faa | 2015-11-04 20:19:42 +0300 | [diff] [blame] | 3498 | |
Hani Benhabiles | b21631f | 2014-05-27 23:39:37 +0100 | [diff] [blame] | 3499 | snapshot = snapshots; |
| 3500 | while (snapshot) { |
| 3501 | char *completion = snapshot->value->name; |
| 3502 | if (!strncmp(str, completion, len)) { |
| 3503 | readline_add_completion(rs, completion); |
| 3504 | } |
| 3505 | completion = snapshot->value->id; |
| 3506 | if (!strncmp(str, completion, len)) { |
| 3507 | readline_add_completion(rs, completion); |
| 3508 | } |
| 3509 | snapshot = snapshot->next; |
| 3510 | } |
| 3511 | qapi_free_SnapshotInfoList(snapshots); |
| 3512 | } |
| 3513 | |
| 3514 | } |
| 3515 | |
| 3516 | void delvm_completion(ReadLineState *rs, int nb_args, const char *str) |
| 3517 | { |
| 3518 | if (nb_args == 2) { |
| 3519 | vm_completion(rs, str); |
| 3520 | } |
| 3521 | } |
| 3522 | |
| 3523 | void loadvm_completion(ReadLineState *rs, int nb_args, const char *str) |
| 3524 | { |
| 3525 | if (nb_args == 2) { |
| 3526 | vm_completion(rs, str); |
| 3527 | } |
| 3528 | } |
| 3529 | |
Wenchao Xia | c35b640 | 2013-08-27 20:38:24 +0800 | [diff] [blame] | 3530 | static void monitor_find_completion_by_table(Monitor *mon, |
| 3531 | const mon_cmd_t *cmd_table, |
| 3532 | char **args, |
| 3533 | int nb_args) |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 3534 | { |
| 3535 | const char *cmdname; |
Wenchao Xia | c35b640 | 2013-08-27 20:38:24 +0800 | [diff] [blame] | 3536 | int i; |
Markus Armbruster | fea68bb | 2014-10-07 13:59:10 +0200 | [diff] [blame] | 3537 | const char *ptype, *str, *name; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3538 | const mon_cmd_t *cmd; |
Max Reitz | da27a00 | 2016-03-16 19:54:29 +0100 | [diff] [blame] | 3539 | BlockBackend *blk = NULL; |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 3540 | |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 3541 | if (nb_args <= 1) { |
| 3542 | /* command completion */ |
| 3543 | if (nb_args == 0) |
| 3544 | cmdname = ""; |
| 3545 | else |
| 3546 | cmdname = args[0]; |
Wenchao Xia | d2674b2 | 2013-08-27 20:38:16 +0800 | [diff] [blame] | 3547 | readline_set_completion_index(mon->rs, strlen(cmdname)); |
Wenchao Xia | c35b640 | 2013-08-27 20:38:24 +0800 | [diff] [blame] | 3548 | for (cmd = cmd_table; cmd->name != NULL; cmd++) { |
Wenchao Xia | cd5c6bb | 2013-08-27 20:38:13 +0800 | [diff] [blame] | 3549 | cmd_completion(mon, cmdname, cmd->name); |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 3550 | } |
| 3551 | } else { |
| 3552 | /* find the command */ |
Wenchao Xia | c35b640 | 2013-08-27 20:38:24 +0800 | [diff] [blame] | 3553 | for (cmd = cmd_table; cmd->name != NULL; cmd++) { |
Jan Kiszka | 03a6348 | 2010-06-16 00:38:33 +0200 | [diff] [blame] | 3554 | if (compare_cmd(args[0], cmd->name)) { |
| 3555 | break; |
| 3556 | } |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 3557 | } |
Jan Kiszka | 03a6348 | 2010-06-16 00:38:33 +0200 | [diff] [blame] | 3558 | if (!cmd->name) { |
Wenchao Xia | c35b640 | 2013-08-27 20:38:24 +0800 | [diff] [blame] | 3559 | return; |
Jan Kiszka | 03a6348 | 2010-06-16 00:38:33 +0200 | [diff] [blame] | 3560 | } |
| 3561 | |
Wenchao Xia | d903a77 | 2013-08-27 20:38:25 +0800 | [diff] [blame] | 3562 | if (cmd->sub_table) { |
| 3563 | /* do the job again */ |
Stefan Weil | e7ae771 | 2015-03-08 19:30:01 +0100 | [diff] [blame] | 3564 | monitor_find_completion_by_table(mon, cmd->sub_table, |
| 3565 | &args[1], nb_args - 1); |
| 3566 | return; |
Wenchao Xia | d903a77 | 2013-08-27 20:38:25 +0800 | [diff] [blame] | 3567 | } |
Hani Benhabiles | bfa40f7 | 2014-04-13 16:25:06 +0100 | [diff] [blame] | 3568 | if (cmd->command_completion) { |
Stefan Weil | e7ae771 | 2015-03-08 19:30:01 +0100 | [diff] [blame] | 3569 | cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]); |
| 3570 | return; |
Hani Benhabiles | bfa40f7 | 2014-04-13 16:25:06 +0100 | [diff] [blame] | 3571 | } |
Wenchao Xia | d903a77 | 2013-08-27 20:38:25 +0800 | [diff] [blame] | 3572 | |
Luiz Capitulino | 4d76d2b | 2009-08-28 15:27:09 -0300 | [diff] [blame] | 3573 | ptype = next_arg_type(cmd->args_type); |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 3574 | for(i = 0; i < nb_args - 2; i++) { |
| 3575 | if (*ptype != '\0') { |
Luiz Capitulino | 4d76d2b | 2009-08-28 15:27:09 -0300 | [diff] [blame] | 3576 | ptype = next_arg_type(ptype); |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 3577 | while (*ptype == '?') |
Luiz Capitulino | 4d76d2b | 2009-08-28 15:27:09 -0300 | [diff] [blame] | 3578 | ptype = next_arg_type(ptype); |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 3579 | } |
| 3580 | } |
| 3581 | str = args[nb_args - 1]; |
Kevin Wolf | 48fe86f | 2014-11-12 16:24:02 +0100 | [diff] [blame] | 3582 | while (*ptype == '-' && ptype[1] != '\0') { |
Jan Kiszka | 3b6dbf2 | 2010-06-16 00:38:34 +0200 | [diff] [blame] | 3583 | ptype = next_arg_type(ptype); |
Blue Swirl | 2a1704a | 2009-08-23 20:10:28 +0000 | [diff] [blame] | 3584 | } |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 3585 | switch(*ptype) { |
| 3586 | case 'F': |
| 3587 | /* file completion */ |
Wenchao Xia | d2674b2 | 2013-08-27 20:38:16 +0800 | [diff] [blame] | 3588 | readline_set_completion_index(mon->rs, strlen(str)); |
Wenchao Xia | cb8f68b | 2013-08-27 20:38:14 +0800 | [diff] [blame] | 3589 | file_completion(mon, str); |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 3590 | break; |
| 3591 | case 'B': |
| 3592 | /* block device name completion */ |
Wenchao Xia | 599a926 | 2013-08-27 20:38:15 +0800 | [diff] [blame] | 3593 | readline_set_completion_index(mon->rs, strlen(str)); |
Max Reitz | da27a00 | 2016-03-16 19:54:29 +0100 | [diff] [blame] | 3594 | while ((blk = blk_next(blk)) != NULL) { |
| 3595 | name = blk_name(blk); |
Markus Armbruster | fea68bb | 2014-10-07 13:59:10 +0200 | [diff] [blame] | 3596 | if (str[0] == '\0' || |
| 3597 | !strncmp(name, str, strlen(str))) { |
| 3598 | readline_add_completion(mon->rs, name); |
| 3599 | } |
| 3600 | } |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 3601 | break; |
bellard | 7fe4848 | 2004-10-09 18:08:01 +0000 | [diff] [blame] | 3602 | case 's': |
Wenchao Xia | 129be00 | 2013-08-27 20:38:26 +0800 | [diff] [blame] | 3603 | case 'S': |
Hani Benhabiles | 29136cd | 2014-05-07 23:41:27 +0100 | [diff] [blame] | 3604 | if (!strcmp(cmd->name, "help|?")) { |
Wenchao Xia | 7ca0e06 | 2013-08-27 20:38:27 +0800 | [diff] [blame] | 3605 | monitor_find_completion_by_table(mon, cmd_table, |
| 3606 | &args[1], nb_args - 1); |
bellard | 7fe4848 | 2004-10-09 18:08:01 +0000 | [diff] [blame] | 3607 | } |
| 3608 | break; |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 3609 | default: |
| 3610 | break; |
| 3611 | } |
| 3612 | } |
Wenchao Xia | c35b640 | 2013-08-27 20:38:24 +0800 | [diff] [blame] | 3613 | } |
| 3614 | |
Stefan Hajnoczi | c60bf33 | 2013-11-14 11:54:14 +0100 | [diff] [blame] | 3615 | static void monitor_find_completion(void *opaque, |
Wenchao Xia | c35b640 | 2013-08-27 20:38:24 +0800 | [diff] [blame] | 3616 | const char *cmdline) |
| 3617 | { |
Stefan Hajnoczi | c60bf33 | 2013-11-14 11:54:14 +0100 | [diff] [blame] | 3618 | Monitor *mon = opaque; |
Wenchao Xia | c35b640 | 2013-08-27 20:38:24 +0800 | [diff] [blame] | 3619 | char *args[MAX_ARGS]; |
| 3620 | int nb_args, len; |
| 3621 | |
| 3622 | /* 1. parse the cmdline */ |
| 3623 | if (parse_cmdline(cmdline, &nb_args, args) < 0) { |
| 3624 | return; |
| 3625 | } |
Wenchao Xia | c35b640 | 2013-08-27 20:38:24 +0800 | [diff] [blame] | 3626 | |
| 3627 | /* if the line ends with a space, it means we want to complete the |
| 3628 | next arg */ |
| 3629 | len = strlen(cmdline); |
| 3630 | if (len > 0 && qemu_isspace(cmdline[len - 1])) { |
| 3631 | if (nb_args >= MAX_ARGS) { |
| 3632 | goto cleanup; |
| 3633 | } |
| 3634 | args[nb_args++] = g_strdup(""); |
| 3635 | } |
| 3636 | |
| 3637 | /* 2. auto complete according to args */ |
| 3638 | monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args); |
Jan Kiszka | 03a6348 | 2010-06-16 00:38:33 +0200 | [diff] [blame] | 3639 | |
| 3640 | cleanup: |
Wenchao Xia | dcc70cd | 2013-08-27 20:38:22 +0800 | [diff] [blame] | 3641 | free_cmdline_args(args, nb_args); |
bellard | 81d0912 | 2004-07-14 17:21:37 +0000 | [diff] [blame] | 3642 | } |
| 3643 | |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 3644 | static int monitor_can_read(void *opaque) |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 3645 | { |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 3646 | Monitor *mon = opaque; |
| 3647 | |
Jan Kiszka | c62313b | 2009-12-04 14:05:29 +0100 | [diff] [blame] | 3648 | return (mon->suspend_cnt == 0) ? 1 : 0; |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 3649 | } |
| 3650 | |
Marc-André Lureau | d79bedf | 2016-09-12 13:18:59 +0400 | [diff] [blame] | 3651 | static bool invalid_qmp_mode(const Monitor *mon, const char *cmd, |
Markus Armbruster | 4086182 | 2015-05-29 10:27:16 +0200 | [diff] [blame] | 3652 | Error **errp) |
Luiz Capitulino | 09069b1 | 2010-02-04 18:10:06 -0200 | [diff] [blame] | 3653 | { |
Marc-André Lureau | d79bedf | 2016-09-12 13:18:59 +0400 | [diff] [blame] | 3654 | bool is_cap = g_str_equal(cmd, "qmp_capabilities"); |
Markus Armbruster | f994b25 | 2015-03-06 19:51:51 +0100 | [diff] [blame] | 3655 | |
| 3656 | if (is_cap && mon->qmp.in_command_mode) { |
Markus Armbruster | 4086182 | 2015-05-29 10:27:16 +0200 | [diff] [blame] | 3657 | error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND, |
| 3658 | "Capabilities negotiation is already complete, command " |
Marc-André Lureau | d79bedf | 2016-09-12 13:18:59 +0400 | [diff] [blame] | 3659 | "'%s' ignored", cmd); |
Eric Blake | 2d5a834 | 2015-04-15 09:19:23 -0600 | [diff] [blame] | 3660 | return true; |
| 3661 | } |
Markus Armbruster | f994b25 | 2015-03-06 19:51:51 +0100 | [diff] [blame] | 3662 | if (!is_cap && !mon->qmp.in_command_mode) { |
Markus Armbruster | 4086182 | 2015-05-29 10:27:16 +0200 | [diff] [blame] | 3663 | error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND, |
| 3664 | "Expecting capabilities negotiation with " |
Marc-André Lureau | d79bedf | 2016-09-12 13:18:59 +0400 | [diff] [blame] | 3665 | "'qmp_capabilities' before command '%s'", cmd); |
Eric Blake | 2d5a834 | 2015-04-15 09:19:23 -0600 | [diff] [blame] | 3666 | return true; |
| 3667 | } |
| 3668 | return false; |
Luiz Capitulino | 09069b1 | 2010-02-04 18:10:06 -0200 | [diff] [blame] | 3669 | } |
| 3670 | |
Luiz Capitulino | 2dbc8db | 2010-05-26 16:13:09 -0300 | [diff] [blame] | 3671 | /* |
Luiz Capitulino | c917c8f | 2010-05-31 17:28:01 -0300 | [diff] [blame] | 3672 | * Input object checking rules |
| 3673 | * |
| 3674 | * 1. Input object must be a dict |
| 3675 | * 2. The "execute" key must exist |
| 3676 | * 3. The "execute" key must be a string |
| 3677 | * 4. If the "arguments" key exists, it must be a dict |
| 3678 | * 5. If the "id" key exists, it can be anything (ie. json-value) |
| 3679 | * 6. Any argument not listed above is considered invalid |
| 3680 | */ |
Markus Armbruster | ba0510a | 2015-03-02 18:41:43 +0100 | [diff] [blame] | 3681 | static QDict *qmp_check_input_obj(QObject *input_obj, Error **errp) |
Luiz Capitulino | c917c8f | 2010-05-31 17:28:01 -0300 | [diff] [blame] | 3682 | { |
| 3683 | const QDictEntry *ent; |
| 3684 | int has_exec_key = 0; |
| 3685 | QDict *input_dict; |
| 3686 | |
| 3687 | if (qobject_type(input_obj) != QTYPE_QDICT) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 3688 | error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "object"); |
Luiz Capitulino | c917c8f | 2010-05-31 17:28:01 -0300 | [diff] [blame] | 3689 | return NULL; |
| 3690 | } |
| 3691 | |
| 3692 | input_dict = qobject_to_qdict(input_obj); |
| 3693 | |
| 3694 | for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){ |
| 3695 | const char *arg_name = qdict_entry_key(ent); |
| 3696 | const QObject *arg_obj = qdict_entry_value(ent); |
| 3697 | |
| 3698 | if (!strcmp(arg_name, "execute")) { |
| 3699 | if (qobject_type(arg_obj) != QTYPE_QSTRING) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 3700 | error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER, |
| 3701 | "execute", "string"); |
Luiz Capitulino | c917c8f | 2010-05-31 17:28:01 -0300 | [diff] [blame] | 3702 | return NULL; |
| 3703 | } |
| 3704 | has_exec_key = 1; |
| 3705 | } else if (!strcmp(arg_name, "arguments")) { |
| 3706 | if (qobject_type(arg_obj) != QTYPE_QDICT) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 3707 | error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER, |
| 3708 | "arguments", "object"); |
Luiz Capitulino | c917c8f | 2010-05-31 17:28:01 -0300 | [diff] [blame] | 3709 | return NULL; |
| 3710 | } |
Markus Armbruster | 779cec4 | 2015-06-08 10:44:30 +0200 | [diff] [blame] | 3711 | } else if (!strcmp(arg_name, "id")) { |
| 3712 | /* Any string is acceptable as "id", so nothing to check */ |
Luiz Capitulino | c917c8f | 2010-05-31 17:28:01 -0300 | [diff] [blame] | 3713 | } else { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 3714 | error_setg(errp, QERR_QMP_EXTRA_MEMBER, arg_name); |
Luiz Capitulino | c917c8f | 2010-05-31 17:28:01 -0300 | [diff] [blame] | 3715 | return NULL; |
| 3716 | } |
| 3717 | } |
| 3718 | |
| 3719 | if (!has_exec_key) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 3720 | error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "execute"); |
Luiz Capitulino | c917c8f | 2010-05-31 17:28:01 -0300 | [diff] [blame] | 3721 | return NULL; |
| 3722 | } |
| 3723 | |
| 3724 | return input_dict; |
| 3725 | } |
| 3726 | |
Paolo Bonzini | 95385fe | 2015-11-25 22:23:31 +0100 | [diff] [blame] | 3727 | static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens) |
Luiz Capitulino | 5fa737a | 2009-11-26 22:59:01 -0200 | [diff] [blame] | 3728 | { |
Marc-André Lureau | 5c678ee | 2016-09-12 13:19:10 +0400 | [diff] [blame] | 3729 | QObject *req, *rsp = NULL, *id = NULL; |
| 3730 | QDict *qdict = NULL; |
Luiz Capitulino | 40e5a01 | 2011-10-21 16:15:31 -0200 | [diff] [blame] | 3731 | const char *cmd_name; |
Luiz Capitulino | 5fa737a | 2009-11-26 22:59:01 -0200 | [diff] [blame] | 3732 | Monitor *mon = cur_mon; |
Marc-André Lureau | 5c678ee | 2016-09-12 13:19:10 +0400 | [diff] [blame] | 3733 | Error *err = NULL; |
Luiz Capitulino | 5fa737a | 2009-11-26 22:59:01 -0200 | [diff] [blame] | 3734 | |
Marc-André Lureau | 5c678ee | 2016-09-12 13:19:10 +0400 | [diff] [blame] | 3735 | req = json_parser_parse_err(tokens, NULL, &err); |
| 3736 | if (err || !req || qobject_type(req) != QTYPE_QDICT) { |
| 3737 | if (!err) { |
| 3738 | error_setg(&err, QERR_JSON_PARSING); |
| 3739 | } |
Luiz Capitulino | 5fa737a | 2009-11-26 22:59:01 -0200 | [diff] [blame] | 3740 | goto err_out; |
Luiz Capitulino | 5fa737a | 2009-11-26 22:59:01 -0200 | [diff] [blame] | 3741 | } |
| 3742 | |
Marc-André Lureau | 5c678ee | 2016-09-12 13:19:10 +0400 | [diff] [blame] | 3743 | qdict = qmp_check_input_obj(req, &err); |
| 3744 | if (!qdict) { |
Luiz Capitulino | 5fa737a | 2009-11-26 22:59:01 -0200 | [diff] [blame] | 3745 | goto err_out; |
| 3746 | } |
| 3747 | |
Marc-André Lureau | 5c678ee | 2016-09-12 13:19:10 +0400 | [diff] [blame] | 3748 | id = qdict_get(qdict, "id"); |
| 3749 | qobject_incref(id); |
| 3750 | qdict_del(qdict, "id"); |
Luiz Capitulino | 5fa737a | 2009-11-26 22:59:01 -0200 | [diff] [blame] | 3751 | |
Marc-André Lureau | 5c678ee | 2016-09-12 13:19:10 +0400 | [diff] [blame] | 3752 | cmd_name = qdict_get_str(qdict, "execute"); |
Stefan Hajnoczi | 89bd820 | 2011-09-23 08:23:06 +0100 | [diff] [blame] | 3753 | trace_handle_qmp_command(mon, cmd_name); |
Marc-André Lureau | 5c678ee | 2016-09-12 13:19:10 +0400 | [diff] [blame] | 3754 | |
| 3755 | if (invalid_qmp_mode(mon, cmd_name, &err)) { |
Eric Blake | 2d5a834 | 2015-04-15 09:19:23 -0600 | [diff] [blame] | 3756 | goto err_out; |
| 3757 | } |
Luiz Capitulino | 5fa737a | 2009-11-26 22:59:01 -0200 | [diff] [blame] | 3758 | |
Marc-André Lureau | 5c678ee | 2016-09-12 13:19:10 +0400 | [diff] [blame] | 3759 | rsp = qmp_dispatch(req); |
Luiz Capitulino | 5fa737a | 2009-11-26 22:59:01 -0200 | [diff] [blame] | 3760 | |
Luiz Capitulino | 5fa737a | 2009-11-26 22:59:01 -0200 | [diff] [blame] | 3761 | err_out: |
Marc-André Lureau | 5c678ee | 2016-09-12 13:19:10 +0400 | [diff] [blame] | 3762 | if (err) { |
| 3763 | qdict = qdict_new(); |
| 3764 | qdict_put_obj(qdict, "error", qmp_build_error_object(err)); |
| 3765 | error_free(err); |
| 3766 | rsp = QOBJECT(qdict); |
| 3767 | } |
| 3768 | |
| 3769 | if (rsp) { |
| 3770 | if (id) { |
| 3771 | qdict_put_obj(qobject_to_qdict(rsp), "id", id); |
| 3772 | id = NULL; |
| 3773 | } |
| 3774 | |
| 3775 | monitor_json_emitter(mon, rsp); |
| 3776 | } |
| 3777 | |
| 3778 | qobject_decref(id); |
| 3779 | qobject_decref(rsp); |
| 3780 | qobject_decref(req); |
Luiz Capitulino | 5fa737a | 2009-11-26 22:59:01 -0200 | [diff] [blame] | 3781 | } |
| 3782 | |
Markus Armbruster | c83fe23 | 2015-03-06 19:20:51 +0100 | [diff] [blame] | 3783 | 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] | 3784 | { |
| 3785 | Monitor *old_mon = cur_mon; |
| 3786 | |
| 3787 | cur_mon = opaque; |
| 3788 | |
Markus Armbruster | 74358f2 | 2015-03-06 19:35:59 +0100 | [diff] [blame] | 3789 | json_message_parser_feed(&cur_mon->qmp.parser, (const char *) buf, size); |
Luiz Capitulino | 9b57c02 | 2009-11-26 22:58:58 -0200 | [diff] [blame] | 3790 | |
| 3791 | cur_mon = old_mon; |
| 3792 | } |
| 3793 | |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 3794 | static void monitor_read(void *opaque, const uint8_t *buf, int size) |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 3795 | { |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 3796 | Monitor *old_mon = cur_mon; |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 3797 | int i; |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 3798 | |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 3799 | cur_mon = opaque; |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 3800 | |
aliguori | cde76ee | 2009-03-05 23:01:51 +0000 | [diff] [blame] | 3801 | if (cur_mon->rs) { |
| 3802 | for (i = 0; i < size; i++) |
| 3803 | readline_handle_byte(cur_mon->rs, buf[i]); |
| 3804 | } else { |
| 3805 | if (size == 0 || buf[size - 1] != 0) |
| 3806 | monitor_printf(cur_mon, "corrupted command\n"); |
| 3807 | else |
Markus Armbruster | 7ef6cf6 | 2015-03-06 19:12:36 +0100 | [diff] [blame] | 3808 | handle_hmp_command(cur_mon, (char *)buf); |
aliguori | cde76ee | 2009-03-05 23:01:51 +0000 | [diff] [blame] | 3809 | } |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 3810 | |
| 3811 | cur_mon = old_mon; |
| 3812 | } |
aliguori | d8f4460 | 2008-10-06 13:52:44 +0000 | [diff] [blame] | 3813 | |
Stefan Hajnoczi | c60bf33 | 2013-11-14 11:54:14 +0100 | [diff] [blame] | 3814 | static void monitor_command_cb(void *opaque, const char *cmdline, |
| 3815 | void *readline_opaque) |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 3816 | { |
Stefan Hajnoczi | c60bf33 | 2013-11-14 11:54:14 +0100 | [diff] [blame] | 3817 | Monitor *mon = opaque; |
| 3818 | |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 3819 | monitor_suspend(mon); |
Markus Armbruster | 7ef6cf6 | 2015-03-06 19:12:36 +0100 | [diff] [blame] | 3820 | handle_hmp_command(mon, cmdline); |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 3821 | monitor_resume(mon); |
aliguori | d8f4460 | 2008-10-06 13:52:44 +0000 | [diff] [blame] | 3822 | } |
| 3823 | |
aliguori | cde76ee | 2009-03-05 23:01:51 +0000 | [diff] [blame] | 3824 | int monitor_suspend(Monitor *mon) |
aliguori | d8f4460 | 2008-10-06 13:52:44 +0000 | [diff] [blame] | 3825 | { |
aliguori | cde76ee | 2009-03-05 23:01:51 +0000 | [diff] [blame] | 3826 | if (!mon->rs) |
| 3827 | return -ENOTTY; |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 3828 | mon->suspend_cnt++; |
aliguori | cde76ee | 2009-03-05 23:01:51 +0000 | [diff] [blame] | 3829 | return 0; |
aliguori | d8f4460 | 2008-10-06 13:52:44 +0000 | [diff] [blame] | 3830 | } |
| 3831 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 3832 | void monitor_resume(Monitor *mon) |
aliguori | d8f4460 | 2008-10-06 13:52:44 +0000 | [diff] [blame] | 3833 | { |
aliguori | cde76ee | 2009-03-05 23:01:51 +0000 | [diff] [blame] | 3834 | if (!mon->rs) |
| 3835 | return; |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 3836 | if (--mon->suspend_cnt == 0) |
| 3837 | readline_show_prompt(mon->rs); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 3838 | } |
| 3839 | |
Luiz Capitulino | ca9567e | 2010-02-04 18:10:04 -0200 | [diff] [blame] | 3840 | static QObject *get_qmp_greeting(void) |
| 3841 | { |
Luiz Capitulino | b9c15f1 | 2011-08-26 17:38:13 -0300 | [diff] [blame] | 3842 | QObject *ver = NULL; |
Luiz Capitulino | ca9567e | 2010-02-04 18:10:04 -0200 | [diff] [blame] | 3843 | |
Markus Armbruster | 7fad30f | 2015-09-16 13:06:19 +0200 | [diff] [blame] | 3844 | qmp_marshal_query_version(NULL, &ver, NULL); |
Marc-André Lureau | c823501 | 2016-09-12 13:19:04 +0400 | [diff] [blame] | 3845 | |
| 3846 | return qobject_from_jsonf("{'QMP': {'version': %p, 'capabilities': []}}", |
| 3847 | ver); |
Luiz Capitulino | ca9567e | 2010-02-04 18:10:04 -0200 | [diff] [blame] | 3848 | } |
| 3849 | |
Markus Armbruster | c83fe23 | 2015-03-06 19:20:51 +0100 | [diff] [blame] | 3850 | static void monitor_qmp_event(void *opaque, int event) |
Luiz Capitulino | 9b57c02 | 2009-11-26 22:58:58 -0200 | [diff] [blame] | 3851 | { |
Luiz Capitulino | 47116d1 | 2010-02-08 17:01:30 -0200 | [diff] [blame] | 3852 | QObject *data; |
| 3853 | Monitor *mon = opaque; |
Luiz Capitulino | 9b57c02 | 2009-11-26 22:58:58 -0200 | [diff] [blame] | 3854 | |
Luiz Capitulino | 47116d1 | 2010-02-08 17:01:30 -0200 | [diff] [blame] | 3855 | switch (event) { |
| 3856 | case CHR_EVENT_OPENED: |
Markus Armbruster | f994b25 | 2015-03-06 19:51:51 +0100 | [diff] [blame] | 3857 | mon->qmp.in_command_mode = false; |
Luiz Capitulino | ca9567e | 2010-02-04 18:10:04 -0200 | [diff] [blame] | 3858 | data = get_qmp_greeting(); |
Luiz Capitulino | 9b57c02 | 2009-11-26 22:58:58 -0200 | [diff] [blame] | 3859 | monitor_json_emitter(mon, data); |
| 3860 | qobject_decref(data); |
Corey Bryant | efb87c1 | 2012-08-14 16:43:48 -0400 | [diff] [blame] | 3861 | mon_refcount++; |
Luiz Capitulino | 47116d1 | 2010-02-08 17:01:30 -0200 | [diff] [blame] | 3862 | break; |
| 3863 | case CHR_EVENT_CLOSED: |
Markus Armbruster | 74358f2 | 2015-03-06 19:35:59 +0100 | [diff] [blame] | 3864 | json_message_parser_destroy(&mon->qmp.parser); |
| 3865 | json_message_parser_init(&mon->qmp.parser, handle_qmp_command); |
Corey Bryant | efb87c1 | 2012-08-14 16:43:48 -0400 | [diff] [blame] | 3866 | mon_refcount--; |
| 3867 | monitor_fdsets_cleanup(); |
Luiz Capitulino | 47116d1 | 2010-02-08 17:01:30 -0200 | [diff] [blame] | 3868 | break; |
Luiz Capitulino | 9b57c02 | 2009-11-26 22:58:58 -0200 | [diff] [blame] | 3869 | } |
| 3870 | } |
| 3871 | |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 3872 | static void monitor_event(void *opaque, int event) |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 3873 | { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 3874 | Monitor *mon = opaque; |
| 3875 | |
aliguori | 2724b18 | 2009-03-05 23:01:47 +0000 | [diff] [blame] | 3876 | switch (event) { |
| 3877 | case CHR_EVENT_MUX_IN: |
Paolo Bonzini | 6cff3e8 | 2014-06-18 08:43:59 +0200 | [diff] [blame] | 3878 | qemu_mutex_lock(&mon->out_lock); |
Gerd Hoffmann | a7aec5d | 2009-09-10 10:58:54 +0200 | [diff] [blame] | 3879 | mon->mux_out = 0; |
Paolo Bonzini | 6cff3e8 | 2014-06-18 08:43:59 +0200 | [diff] [blame] | 3880 | qemu_mutex_unlock(&mon->out_lock); |
Gerd Hoffmann | a7aec5d | 2009-09-10 10:58:54 +0200 | [diff] [blame] | 3881 | if (mon->reset_seen) { |
| 3882 | readline_restart(mon->rs); |
| 3883 | monitor_resume(mon); |
| 3884 | monitor_flush(mon); |
| 3885 | } else { |
| 3886 | mon->suspend_cnt = 0; |
| 3887 | } |
aliguori | 2724b18 | 2009-03-05 23:01:47 +0000 | [diff] [blame] | 3888 | break; |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 3889 | |
aliguori | 2724b18 | 2009-03-05 23:01:47 +0000 | [diff] [blame] | 3890 | case CHR_EVENT_MUX_OUT: |
Gerd Hoffmann | a7aec5d | 2009-09-10 10:58:54 +0200 | [diff] [blame] | 3891 | if (mon->reset_seen) { |
| 3892 | if (mon->suspend_cnt == 0) { |
| 3893 | monitor_printf(mon, "\n"); |
| 3894 | } |
| 3895 | monitor_flush(mon); |
| 3896 | monitor_suspend(mon); |
| 3897 | } else { |
| 3898 | mon->suspend_cnt++; |
| 3899 | } |
Paolo Bonzini | 6cff3e8 | 2014-06-18 08:43:59 +0200 | [diff] [blame] | 3900 | qemu_mutex_lock(&mon->out_lock); |
Gerd Hoffmann | a7aec5d | 2009-09-10 10:58:54 +0200 | [diff] [blame] | 3901 | mon->mux_out = 1; |
Paolo Bonzini | 6cff3e8 | 2014-06-18 08:43:59 +0200 | [diff] [blame] | 3902 | qemu_mutex_unlock(&mon->out_lock); |
aliguori | 2724b18 | 2009-03-05 23:01:47 +0000 | [diff] [blame] | 3903 | break; |
| 3904 | |
Amit Shah | b6b8df5 | 2009-10-07 18:31:16 +0530 | [diff] [blame] | 3905 | case CHR_EVENT_OPENED: |
aliguori | 2724b18 | 2009-03-05 23:01:47 +0000 | [diff] [blame] | 3906 | monitor_printf(mon, "QEMU %s monitor - type 'help' for more " |
| 3907 | "information\n", QEMU_VERSION); |
Gerd Hoffmann | a7aec5d | 2009-09-10 10:58:54 +0200 | [diff] [blame] | 3908 | if (!mon->mux_out) { |
Stratos Psomadakis | e5554e2 | 2014-09-15 15:34:57 +0300 | [diff] [blame] | 3909 | readline_restart(mon->rs); |
aliguori | 2724b18 | 2009-03-05 23:01:47 +0000 | [diff] [blame] | 3910 | readline_show_prompt(mon->rs); |
Gerd Hoffmann | a7aec5d | 2009-09-10 10:58:54 +0200 | [diff] [blame] | 3911 | } |
| 3912 | mon->reset_seen = 1; |
Corey Bryant | efb87c1 | 2012-08-14 16:43:48 -0400 | [diff] [blame] | 3913 | mon_refcount++; |
| 3914 | break; |
| 3915 | |
| 3916 | case CHR_EVENT_CLOSED: |
| 3917 | mon_refcount--; |
| 3918 | monitor_fdsets_cleanup(); |
aliguori | 2724b18 | 2009-03-05 23:01:47 +0000 | [diff] [blame] | 3919 | break; |
| 3920 | } |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 3921 | } |
| 3922 | |
Wayne Xia | 816f892 | 2011-10-12 11:32:41 +0800 | [diff] [blame] | 3923 | static int |
| 3924 | compare_mon_cmd(const void *a, const void *b) |
| 3925 | { |
| 3926 | return strcmp(((const mon_cmd_t *)a)->name, |
| 3927 | ((const mon_cmd_t *)b)->name); |
| 3928 | } |
| 3929 | |
| 3930 | static void sortcmdlist(void) |
| 3931 | { |
| 3932 | int array_num; |
| 3933 | int elem_size = sizeof(mon_cmd_t); |
| 3934 | |
| 3935 | array_num = sizeof(mon_cmds)/elem_size-1; |
| 3936 | qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd); |
| 3937 | |
| 3938 | array_num = sizeof(info_cmds)/elem_size-1; |
| 3939 | qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd); |
| 3940 | } |
| 3941 | |
Stefan Hajnoczi | c60bf33 | 2013-11-14 11:54:14 +0100 | [diff] [blame] | 3942 | /* These functions just adapt the readline interface in a typesafe way. We |
| 3943 | * could cast function pointers but that discards compiler checks. |
| 3944 | */ |
Stefan Weil | d5d1507 | 2014-01-25 18:18:23 +0100 | [diff] [blame] | 3945 | static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque, |
| 3946 | const char *fmt, ...) |
Stefan Hajnoczi | c60bf33 | 2013-11-14 11:54:14 +0100 | [diff] [blame] | 3947 | { |
| 3948 | va_list ap; |
| 3949 | va_start(ap, fmt); |
| 3950 | monitor_vprintf(opaque, fmt, ap); |
| 3951 | va_end(ap); |
| 3952 | } |
| 3953 | |
| 3954 | static void monitor_readline_flush(void *opaque) |
| 3955 | { |
| 3956 | monitor_flush(opaque); |
| 3957 | } |
| 3958 | |
Paolo Bonzini | d622cb5 | 2014-06-18 08:44:00 +0200 | [diff] [blame] | 3959 | static void __attribute__((constructor)) monitor_lock_init(void) |
| 3960 | { |
| 3961 | qemu_mutex_init(&monitor_lock); |
| 3962 | } |
| 3963 | |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 3964 | void monitor_init(CharDriverState *chr, int flags) |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 3965 | { |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 3966 | static int is_first_init = 1; |
aliguori | 8712716 | 2009-03-05 23:01:29 +0000 | [diff] [blame] | 3967 | Monitor *mon; |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 3968 | |
| 3969 | if (is_first_init) { |
Wenchao Xia | 43a14cf | 2014-06-18 08:43:31 +0200 | [diff] [blame] | 3970 | monitor_qapi_event_init(); |
Wenchao Xia | d038317 | 2013-08-27 20:38:18 +0800 | [diff] [blame] | 3971 | sortcmdlist(); |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 3972 | is_first_init = 0; |
| 3973 | } |
aliguori | 8712716 | 2009-03-05 23:01:29 +0000 | [diff] [blame] | 3974 | |
Wenchao Xia | b01fe89 | 2013-08-27 20:38:19 +0800 | [diff] [blame] | 3975 | mon = g_malloc(sizeof(*mon)); |
| 3976 | monitor_data_init(mon); |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 3977 | |
aliguori | 8712716 | 2009-03-05 23:01:29 +0000 | [diff] [blame] | 3978 | mon->chr = chr; |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 3979 | mon->flags = flags; |
aliguori | cde76ee | 2009-03-05 23:01:51 +0000 | [diff] [blame] | 3980 | if (flags & MONITOR_USE_READLINE) { |
Stefan Hajnoczi | c60bf33 | 2013-11-14 11:54:14 +0100 | [diff] [blame] | 3981 | mon->rs = readline_init(monitor_readline_printf, |
| 3982 | monitor_readline_flush, |
| 3983 | mon, |
| 3984 | monitor_find_completion); |
aliguori | cde76ee | 2009-03-05 23:01:51 +0000 | [diff] [blame] | 3985 | monitor_read_command(mon, 0); |
| 3986 | } |
aliguori | 8712716 | 2009-03-05 23:01:29 +0000 | [diff] [blame] | 3987 | |
Markus Armbruster | 9f3982f | 2015-03-06 19:56:38 +0100 | [diff] [blame] | 3988 | if (monitor_is_qmp(mon)) { |
Markus Armbruster | c83fe23 | 2015-03-06 19:20:51 +0100 | [diff] [blame] | 3989 | qemu_chr_add_handlers(chr, monitor_can_read, monitor_qmp_read, |
| 3990 | monitor_qmp_event, mon); |
Anthony Liguori | 15f3151 | 2011-08-15 11:17:35 -0500 | [diff] [blame] | 3991 | qemu_chr_fe_set_echo(chr, true); |
Markus Armbruster | 74358f2 | 2015-03-06 19:35:59 +0100 | [diff] [blame] | 3992 | json_message_parser_init(&mon->qmp.parser, handle_qmp_command); |
Luiz Capitulino | 9b57c02 | 2009-11-26 22:58:58 -0200 | [diff] [blame] | 3993 | } else { |
| 3994 | qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, |
| 3995 | monitor_event, mon); |
| 3996 | } |
aliguori | 8712716 | 2009-03-05 23:01:29 +0000 | [diff] [blame] | 3997 | |
Paolo Bonzini | d622cb5 | 2014-06-18 08:44:00 +0200 | [diff] [blame] | 3998 | qemu_mutex_lock(&monitor_lock); |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3999 | QLIST_INSERT_HEAD(&mon_list, mon, entry); |
Paolo Bonzini | d622cb5 | 2014-06-18 08:44:00 +0200 | [diff] [blame] | 4000 | qemu_mutex_unlock(&monitor_lock); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 4001 | } |
| 4002 | |
Marc-André Lureau | 2ef4571 | 2016-08-01 15:23:42 +0400 | [diff] [blame] | 4003 | void monitor_cleanup(void) |
| 4004 | { |
| 4005 | Monitor *mon, *next; |
| 4006 | |
| 4007 | qemu_mutex_lock(&monitor_lock); |
| 4008 | QLIST_FOREACH_SAFE(mon, &mon_list, entry, next) { |
| 4009 | QLIST_REMOVE(mon, entry); |
| 4010 | monitor_data_destroy(mon); |
| 4011 | g_free(mon); |
| 4012 | } |
| 4013 | qemu_mutex_unlock(&monitor_lock); |
| 4014 | } |
| 4015 | |
Stefan Hajnoczi | c60bf33 | 2013-11-14 11:54:14 +0100 | [diff] [blame] | 4016 | static void bdrv_password_cb(void *opaque, const char *password, |
| 4017 | void *readline_opaque) |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 4018 | { |
Stefan Hajnoczi | c60bf33 | 2013-11-14 11:54:14 +0100 | [diff] [blame] | 4019 | Monitor *mon = opaque; |
| 4020 | BlockDriverState *bs = readline_opaque; |
aliguori | bb5fc20 | 2009-03-05 23:01:15 +0000 | [diff] [blame] | 4021 | int ret = 0; |
Markus Armbruster | 4d2855a | 2015-01-29 10:37:00 +0100 | [diff] [blame] | 4022 | Error *local_err = NULL; |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 4023 | |
Markus Armbruster | 4d2855a | 2015-01-29 10:37:00 +0100 | [diff] [blame] | 4024 | bdrv_add_key(bs, password, &local_err); |
| 4025 | if (local_err) { |
Markus Armbruster | 193227f | 2015-12-18 16:35:06 +0100 | [diff] [blame] | 4026 | error_report_err(local_err); |
aliguori | bb5fc20 | 2009-03-05 23:01:15 +0000 | [diff] [blame] | 4027 | ret = -EPERM; |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 4028 | } |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 4029 | if (mon->password_completion_cb) |
| 4030 | mon->password_completion_cb(mon->password_opaque, ret); |
aliguori | bb5fc20 | 2009-03-05 23:01:15 +0000 | [diff] [blame] | 4031 | |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 4032 | monitor_read_command(mon, 1); |
bellard | 9dc39cb | 2004-03-14 21:38:27 +0000 | [diff] [blame] | 4033 | } |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 4034 | |
Luiz Capitulino | 0bbc47b | 2010-02-10 23:50:01 -0200 | [diff] [blame] | 4035 | int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs, |
Markus Armbruster | 097310b | 2014-10-07 13:59:15 +0200 | [diff] [blame] | 4036 | BlockCompletionFunc *completion_cb, |
Luiz Capitulino | 0bbc47b | 2010-02-10 23:50:01 -0200 | [diff] [blame] | 4037 | void *opaque) |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 4038 | { |
aliguori | cde76ee | 2009-03-05 23:01:51 +0000 | [diff] [blame] | 4039 | int err; |
| 4040 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 4041 | monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs), |
| 4042 | bdrv_get_encrypted_filename(bs)); |
aliguori | bb5fc20 | 2009-03-05 23:01:15 +0000 | [diff] [blame] | 4043 | |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 4044 | mon->password_completion_cb = completion_cb; |
| 4045 | mon->password_opaque = opaque; |
aliguori | bb5fc20 | 2009-03-05 23:01:15 +0000 | [diff] [blame] | 4046 | |
aliguori | cde76ee | 2009-03-05 23:01:51 +0000 | [diff] [blame] | 4047 | err = monitor_read_password(mon, bdrv_password_cb, bs); |
| 4048 | |
| 4049 | if (err && completion_cb) |
| 4050 | completion_cb(opaque, err); |
Luiz Capitulino | 0bbc47b | 2010-02-10 23:50:01 -0200 | [diff] [blame] | 4051 | |
| 4052 | return err; |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 4053 | } |
Luiz Capitulino | e42e818 | 2011-11-22 17:58:31 -0200 | [diff] [blame] | 4054 | |
| 4055 | int monitor_read_block_device_key(Monitor *mon, const char *device, |
Markus Armbruster | 097310b | 2014-10-07 13:59:15 +0200 | [diff] [blame] | 4056 | BlockCompletionFunc *completion_cb, |
Luiz Capitulino | e42e818 | 2011-11-22 17:58:31 -0200 | [diff] [blame] | 4057 | void *opaque) |
| 4058 | { |
Markus Armbruster | 9b14e0e | 2015-03-12 17:26:48 +0100 | [diff] [blame] | 4059 | Error *err = NULL; |
Fam Zheng | 5560625 | 2015-03-02 19:36:46 +0800 | [diff] [blame] | 4060 | BlockBackend *blk; |
Luiz Capitulino | e42e818 | 2011-11-22 17:58:31 -0200 | [diff] [blame] | 4061 | |
Fam Zheng | 5560625 | 2015-03-02 19:36:46 +0800 | [diff] [blame] | 4062 | blk = blk_by_name(device); |
| 4063 | if (!blk) { |
Luiz Capitulino | e42e818 | 2011-11-22 17:58:31 -0200 | [diff] [blame] | 4064 | monitor_printf(mon, "Device not found %s\n", device); |
| 4065 | return -1; |
| 4066 | } |
Max Reitz | 5433c24 | 2015-10-19 17:53:29 +0200 | [diff] [blame] | 4067 | if (!blk_bs(blk)) { |
| 4068 | monitor_printf(mon, "Device '%s' has no medium\n", device); |
| 4069 | return -1; |
| 4070 | } |
Luiz Capitulino | e42e818 | 2011-11-22 17:58:31 -0200 | [diff] [blame] | 4071 | |
Markus Armbruster | 9b14e0e | 2015-03-12 17:26:48 +0100 | [diff] [blame] | 4072 | bdrv_add_key(blk_bs(blk), NULL, &err); |
| 4073 | if (err) { |
| 4074 | error_free(err); |
| 4075 | return monitor_read_bdrv_key_start(mon, blk_bs(blk), completion_cb, opaque); |
| 4076 | } |
| 4077 | |
| 4078 | if (completion_cb) { |
| 4079 | completion_cb(opaque, 0); |
| 4080 | } |
| 4081 | return 0; |
Luiz Capitulino | e42e818 | 2011-11-22 17:58:31 -0200 | [diff] [blame] | 4082 | } |
Paolo Bonzini | 4d45457 | 2012-11-26 16:03:42 +0100 | [diff] [blame] | 4083 | |
| 4084 | QemuOptsList qemu_mon_opts = { |
| 4085 | .name = "mon", |
| 4086 | .implied_opt_name = "chardev", |
| 4087 | .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head), |
| 4088 | .desc = { |
| 4089 | { |
| 4090 | .name = "mode", |
| 4091 | .type = QEMU_OPT_STRING, |
| 4092 | },{ |
| 4093 | .name = "chardev", |
| 4094 | .type = QEMU_OPT_STRING, |
| 4095 | },{ |
| 4096 | .name = "default", |
| 4097 | .type = QEMU_OPT_BOOL, |
| 4098 | },{ |
| 4099 | .name = "pretty", |
| 4100 | .type = QEMU_OPT_BOOL, |
| 4101 | }, |
| 4102 | { /* end of list */ } |
| 4103 | }, |
| 4104 | }; |
Marcelo Tosatti | f2ae8ab | 2014-06-24 18:55:11 -0300 | [diff] [blame] | 4105 | |
| 4106 | #ifndef TARGET_I386 |
| 4107 | void qmp_rtc_reset_reinjection(Error **errp) |
| 4108 | { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 4109 | error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection"); |
Marcelo Tosatti | f2ae8ab | 2014-06-24 18:55:11 -0300 | [diff] [blame] | 4110 | } |
| 4111 | #endif |
Jason J. Herne | 7ee0c3e | 2015-06-26 14:03:16 -0400 | [diff] [blame] | 4112 | |
| 4113 | #ifndef TARGET_S390X |
| 4114 | void qmp_dump_skeys(const char *filename, Error **errp) |
| 4115 | { |
| 4116 | error_setg(errp, QERR_FEATURE_DISABLED, "dump-skeys"); |
| 4117 | } |
| 4118 | #endif |
Peter Xu | ae50a77 | 2016-03-30 17:27:24 +0100 | [diff] [blame] | 4119 | |
| 4120 | #ifndef TARGET_ARM |
| 4121 | GICCapabilityList *qmp_query_gic_capabilities(Error **errp) |
| 4122 | { |
| 4123 | error_setg(errp, QERR_FEATURE_DISABLED, "query-gic-capabilities"); |
| 4124 | return NULL; |
| 4125 | } |
| 4126 | #endif |
Igor Mammedov | d463354 | 2016-06-10 06:29:06 +0530 | [diff] [blame] | 4127 | |
| 4128 | HotpluggableCPUList *qmp_query_hotpluggable_cpus(Error **errp) |
| 4129 | { |
| 4130 | MachineState *ms = MACHINE(qdev_get_machine()); |
| 4131 | MachineClass *mc = MACHINE_GET_CLASS(ms); |
| 4132 | |
| 4133 | if (!mc->query_hotpluggable_cpus) { |
| 4134 | error_setg(errp, QERR_FEATURE_DISABLED, "query-hotpluggable-cpus"); |
| 4135 | return NULL; |
| 4136 | } |
| 4137 | |
| 4138 | return mc->query_hotpluggable_cpus(ms); |
| 4139 | } |