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