Kevin Wolf | 5bce308 | 2019-06-13 17:33:59 +0200 | [diff] [blame] | 1 | /* |
| 2 | * QEMU monitor |
| 3 | * |
| 4 | * Copyright (c) 2003-2004 Fabrice Bellard |
| 5 | * |
| 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 | */ |
| 24 | |
| 25 | #ifndef MONITOR_INTERNAL_H |
| 26 | #define MONITOR_INTERNAL_H |
| 27 | |
| 28 | #include "chardev/char-fe.h" |
| 29 | #include "monitor/monitor.h" |
Kevin Wolf | fa4dcf5 | 2020-01-29 11:22:37 +0100 | [diff] [blame] | 30 | #include "qapi/qapi-types-control.h" |
Kevin Wolf | 5bce308 | 2019-06-13 17:33:59 +0200 | [diff] [blame] | 31 | #include "qapi/qmp/dispatch.h" |
| 32 | #include "qapi/qmp/json-parser.h" |
| 33 | #include "qemu/readline.h" |
Kevin Wolf | 7e3c0de | 2019-06-13 17:34:00 +0200 | [diff] [blame] | 34 | #include "sysemu/iothread.h" |
Kevin Wolf | 5bce308 | 2019-06-13 17:33:59 +0200 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | * Supported types: |
| 38 | * |
| 39 | * 'F' filename |
| 40 | * 'B' block device name |
| 41 | * 's' string (accept optional quote) |
| 42 | * 'S' it just appends the rest of the string (accept optional quote) |
| 43 | * 'O' option string of the form NAME=VALUE,... |
| 44 | * parsed according to QemuOptsList given by its name |
| 45 | * Example: 'device:O' uses qemu_device_opts. |
| 46 | * Restriction: only lists with empty desc are supported |
| 47 | * TODO lift the restriction |
| 48 | * 'i' 32 bit integer |
| 49 | * 'l' target long (32 or 64 bit) |
| 50 | * 'M' Non-negative target long (32 or 64 bit), in user mode the |
| 51 | * value is multiplied by 2^20 (think Mebibyte) |
| 52 | * 'o' octets (aka bytes) |
| 53 | * user mode accepts an optional E, e, P, p, T, t, G, g, M, m, |
| 54 | * K, k suffix, which multiplies the value by 2^60 for suffixes E |
| 55 | * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t, |
| 56 | * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k |
| 57 | * 'T' double |
| 58 | * user mode accepts an optional ms, us, ns suffix, |
| 59 | * which divides the value by 1e3, 1e6, 1e9, respectively |
| 60 | * '/' optional gdb-like print format (like "/10x") |
| 61 | * |
| 62 | * '?' optional type (for all types, except '/') |
| 63 | * '.' other form of optional type (for 'i' and 'l') |
| 64 | * 'b' boolean |
| 65 | * user mode accepts "on" or "off" |
Stefan Reiter | 26fcd76 | 2022-02-25 09:49:47 +0100 | [diff] [blame] | 66 | * '-' optional parameter (eg. '-f'); if followed by a 's', it |
| 67 | * specifies an optional string param (e.g. '-fs' allows '-f foo') |
Kevin Wolf | 5bce308 | 2019-06-13 17:33:59 +0200 | [diff] [blame] | 68 | * |
| 69 | */ |
| 70 | |
| 71 | typedef struct HMPCommand { |
| 72 | const char *name; |
| 73 | const char *args_type; |
| 74 | const char *params; |
| 75 | const char *help; |
| 76 | const char *flags; /* p=preconfig */ |
| 77 | void (*cmd)(Monitor *mon, const QDict *qdict); |
Daniel P. Berrangé | f9429c6 | 2021-10-08 15:09:00 +0100 | [diff] [blame] | 78 | /* |
| 79 | * If implementing a command that takes no arguments and simply |
| 80 | * prints formatted data, then leave @cmd NULL, and then set |
| 81 | * @cmd_info_hrt to the corresponding QMP handler that returns |
| 82 | * the formatted text. |
| 83 | */ |
| 84 | HumanReadableText *(*cmd_info_hrt)(Error **errp); |
Kevin Wolf | bb4b9ea | 2020-10-05 17:58:51 +0200 | [diff] [blame] | 85 | bool coroutine; |
Kevin Wolf | 5bce308 | 2019-06-13 17:33:59 +0200 | [diff] [blame] | 86 | /* |
| 87 | * @sub_table is a list of 2nd level of commands. If it does not exist, |
| 88 | * cmd should be used. If it exists, sub_table[?].cmd should be |
| 89 | * used, and cmd of 1st level plays the role of help function. |
| 90 | */ |
| 91 | struct HMPCommand *sub_table; |
| 92 | void (*command_completion)(ReadLineState *rs, int nb_args, const char *str); |
| 93 | } HMPCommand; |
| 94 | |
| 95 | struct Monitor { |
| 96 | CharBackend chr; |
| 97 | int reset_seen; |
Kevin Wolf | 5bce308 | 2019-06-13 17:33:59 +0200 | [diff] [blame] | 98 | int suspend_cnt; /* Needs to be accessed atomically */ |
Kevin Wolf | 9208241 | 2019-06-13 17:34:03 +0200 | [diff] [blame] | 99 | bool is_qmp; |
Kevin Wolf | 5bce308 | 2019-06-13 17:33:59 +0200 | [diff] [blame] | 100 | bool skip_flush; |
| 101 | bool use_io_thread; |
| 102 | |
Markus Armbruster | ddfb0ba | 2020-05-05 17:29:10 +0200 | [diff] [blame] | 103 | char *mon_cpu_path; |
Kevin Wolf | 5bce308 | 2019-06-13 17:33:59 +0200 | [diff] [blame] | 104 | QTAILQ_ENTRY(Monitor) entry; |
| 105 | |
| 106 | /* |
| 107 | * The per-monitor lock. We can't access guest memory when holding |
| 108 | * the lock. |
| 109 | */ |
| 110 | QemuMutex mon_lock; |
| 111 | |
| 112 | /* |
| 113 | * Members that are protected by the per-monitor lock |
| 114 | */ |
| 115 | QLIST_HEAD(, mon_fd_t) fds; |
Markus Armbruster | 20076f4 | 2020-12-11 18:11:34 +0100 | [diff] [blame] | 116 | GString *outbuf; |
Kevin Wolf | 5bce308 | 2019-06-13 17:33:59 +0200 | [diff] [blame] | 117 | guint out_watch; |
| 118 | /* Read under either BQL or mon_lock, written with BQL+mon_lock. */ |
| 119 | int mux_out; |
| 120 | }; |
| 121 | |
| 122 | struct MonitorHMP { |
| 123 | Monitor common; |
Kevin Wolf | 9208241 | 2019-06-13 17:34:03 +0200 | [diff] [blame] | 124 | bool use_readline; |
Kevin Wolf | 5bce308 | 2019-06-13 17:33:59 +0200 | [diff] [blame] | 125 | /* |
| 126 | * State used only in the thread "owning" the monitor. |
| 127 | * If @use_io_thread, this is @mon_iothread. (This does not actually happen |
| 128 | * in the current state of the code.) |
| 129 | * Else, it's the main thread. |
| 130 | * These members can be safely accessed without locks. |
| 131 | */ |
| 132 | ReadLineState *rs; |
| 133 | }; |
| 134 | |
| 135 | typedef struct { |
| 136 | Monitor common; |
| 137 | JSONMessageParser parser; |
Kevin Wolf | 9208241 | 2019-06-13 17:34:03 +0200 | [diff] [blame] | 138 | bool pretty; |
Kevin Wolf | 5bce308 | 2019-06-13 17:33:59 +0200 | [diff] [blame] | 139 | /* |
| 140 | * When a client connects, we're in capabilities negotiation mode. |
| 141 | * @commands is &qmp_cap_negotiation_commands then. When command |
| 142 | * qmp_capabilities succeeds, we go into command mode, and |
| 143 | * @command becomes &qmp_commands. |
| 144 | */ |
Marc-André Lureau | f0ccc00 | 2020-03-16 18:18:24 +0100 | [diff] [blame] | 145 | const QmpCommandList *commands; |
Kevin Wolf | 5bce308 | 2019-06-13 17:33:59 +0200 | [diff] [blame] | 146 | bool capab_offered[QMP_CAPABILITY__MAX]; /* capabilities offered */ |
| 147 | bool capab[QMP_CAPABILITY__MAX]; /* offered and accepted */ |
| 148 | /* |
| 149 | * Protects qmp request/response queue. |
| 150 | * Take monitor_lock first when you need both. |
| 151 | */ |
| 152 | QemuMutex qmp_queue_lock; |
| 153 | /* Input queue that holds all the parsed QMP requests */ |
| 154 | GQueue *qmp_requests; |
| 155 | } MonitorQMP; |
| 156 | |
Kevin Wolf | 7e3c0de | 2019-06-13 17:34:00 +0200 | [diff] [blame] | 157 | /** |
| 158 | * Is @mon a QMP monitor? |
| 159 | */ |
| 160 | static inline bool monitor_is_qmp(const Monitor *mon) |
| 161 | { |
Kevin Wolf | 9208241 | 2019-06-13 17:34:03 +0200 | [diff] [blame] | 162 | return mon->is_qmp; |
Kevin Wolf | 7e3c0de | 2019-06-13 17:34:00 +0200 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | typedef QTAILQ_HEAD(MonitorList, Monitor) MonitorList; |
| 166 | extern IOThread *mon_iothread; |
Kevin Wolf | 9ce44e2 | 2020-10-05 17:58:50 +0200 | [diff] [blame] | 167 | extern Coroutine *qmp_dispatcher_co; |
| 168 | extern bool qmp_dispatcher_co_shutdown; |
| 169 | extern bool qmp_dispatcher_co_busy; |
Kevin Wolf | 7e3c0de | 2019-06-13 17:34:00 +0200 | [diff] [blame] | 170 | extern QmpCommandList qmp_commands, qmp_cap_negotiation_commands; |
| 171 | extern QemuMutex monitor_lock; |
| 172 | extern MonitorList mon_list; |
| 173 | extern int mon_refcount; |
| 174 | |
Kevin Wolf | ed7bda5 | 2019-06-13 17:34:01 +0200 | [diff] [blame] | 175 | extern HMPCommand hmp_cmds[]; |
| 176 | |
Kevin Wolf | 7e3c0de | 2019-06-13 17:34:00 +0200 | [diff] [blame] | 177 | int monitor_puts(Monitor *mon, const char *str); |
Kevin Wolf | 9208241 | 2019-06-13 17:34:03 +0200 | [diff] [blame] | 178 | void monitor_data_init(Monitor *mon, bool is_qmp, bool skip_flush, |
Kevin Wolf | 7e3c0de | 2019-06-13 17:34:00 +0200 | [diff] [blame] | 179 | bool use_io_thread); |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 180 | void monitor_data_destroy(Monitor *mon); |
Kevin Wolf | 7e3c0de | 2019-06-13 17:34:00 +0200 | [diff] [blame] | 181 | int monitor_can_read(void *opaque); |
| 182 | void monitor_list_append(Monitor *mon); |
| 183 | void monitor_fdsets_cleanup(void); |
| 184 | |
| 185 | void qmp_send_response(MonitorQMP *mon, const QDict *rsp); |
| 186 | void monitor_data_destroy_qmp(MonitorQMP *mon); |
Kevin Wolf | 9ce44e2 | 2020-10-05 17:58:50 +0200 | [diff] [blame] | 187 | void coroutine_fn monitor_qmp_dispatcher_co(void *data); |
Kevin Wolf | 7e3c0de | 2019-06-13 17:34:00 +0200 | [diff] [blame] | 188 | |
Kevin Wolf | 2fc5d01 | 2020-11-13 12:43:24 +0100 | [diff] [blame] | 189 | int get_monitor_def(Monitor *mon, int64_t *pval, const char *name); |
Kevin Wolf | ed7bda5 | 2019-06-13 17:34:01 +0200 | [diff] [blame] | 190 | void help_cmd(Monitor *mon, const char *name); |
| 191 | void handle_hmp_command(MonitorHMP *mon, const char *cmdline); |
| 192 | int hmp_compare_cmd(const char *name, const char *list); |
| 193 | |
Kevin Wolf | 5bce308 | 2019-06-13 17:33:59 +0200 | [diff] [blame] | 194 | #endif |