Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +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 | #include "qemu/osdep.h" |
| 26 | #include "monitor-internal.h" |
| 27 | #include "qapi/error.h" |
Kevin Wolf | f209872 | 2020-02-24 15:30:04 +0100 | [diff] [blame] | 28 | #include "qapi/opts-visitor.h" |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 29 | #include "qapi/qapi-emit-events.h" |
Kevin Wolf | f209872 | 2020-02-24 15:30:04 +0100 | [diff] [blame] | 30 | #include "qapi/qapi-visit-control.h" |
Daniel P. Berrangé | 407bc4b | 2024-11-18 16:12:34 +0100 | [diff] [blame] | 31 | #include "qobject/qdict.h" |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 32 | #include "qemu/error-report.h" |
| 33 | #include "qemu/option.h" |
Philippe Mathieu-Daudé | 32cad1f | 2024-12-03 15:20:13 +0100 | [diff] [blame] | 34 | #include "system/qtest.h" |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 35 | #include "trace.h" |
| 36 | |
| 37 | /* |
| 38 | * To prevent flooding clients, events can be throttled. The |
| 39 | * throttling is calculated globally, rather than per-Monitor |
| 40 | * instance. |
| 41 | */ |
| 42 | typedef struct MonitorQAPIEventState { |
| 43 | QAPIEvent event; /* Throttling state for this event type and... */ |
| 44 | QDict *data; /* ... data, see qapi_event_throttle_equal() */ |
| 45 | QEMUTimer *timer; /* Timer for handling delayed events */ |
| 46 | QDict *qdict; /* Delayed event (if any) */ |
| 47 | } MonitorQAPIEventState; |
| 48 | |
| 49 | typedef struct { |
| 50 | int64_t rate; /* Minimum time (in ns) between two events */ |
| 51 | } MonitorQAPIEventConf; |
| 52 | |
| 53 | /* Shared monitor I/O thread */ |
| 54 | IOThread *mon_iothread; |
| 55 | |
Kevin Wolf | 9ce44e2 | 2020-10-05 17:58:50 +0200 | [diff] [blame] | 56 | /* Coroutine to dispatch the requests received from I/O thread */ |
| 57 | Coroutine *qmp_dispatcher_co; |
| 58 | |
Paolo Bonzini | 0ff2553 | 2023-03-03 12:51:33 +0100 | [diff] [blame] | 59 | /* |
| 60 | * Set to true when the dispatcher coroutine should terminate. Protected |
| 61 | * by monitor_lock. |
| 62 | */ |
Kevin Wolf | 9ce44e2 | 2020-10-05 17:58:50 +0200 | [diff] [blame] | 63 | bool qmp_dispatcher_co_shutdown; |
| 64 | |
| 65 | /* |
Kevin Wolf | e69ee45 | 2020-10-05 17:58:48 +0200 | [diff] [blame] | 66 | * Protects mon_list, monitor_qapi_event_state, coroutine_mon, |
| 67 | * monitor_destroyed. |
| 68 | */ |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 69 | QemuMutex monitor_lock; |
| 70 | static GHashTable *monitor_qapi_event_state; |
Kevin Wolf | e69ee45 | 2020-10-05 17:58:48 +0200 | [diff] [blame] | 71 | static GHashTable *coroutine_mon; /* Maps Coroutine* to Monitor* */ |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 72 | |
| 73 | MonitorList mon_list; |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 74 | static bool monitor_destroyed; |
| 75 | |
Kevin Wolf | 947e474 | 2020-10-05 17:58:44 +0200 | [diff] [blame] | 76 | Monitor *monitor_cur(void) |
| 77 | { |
Kevin Wolf | e69ee45 | 2020-10-05 17:58:48 +0200 | [diff] [blame] | 78 | Monitor *mon; |
| 79 | |
| 80 | qemu_mutex_lock(&monitor_lock); |
| 81 | mon = g_hash_table_lookup(coroutine_mon, qemu_coroutine_self()); |
| 82 | qemu_mutex_unlock(&monitor_lock); |
| 83 | |
| 84 | return mon; |
Kevin Wolf | 947e474 | 2020-10-05 17:58:44 +0200 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Sets a new current monitor and returns the old one. |
Kevin Wolf | e69ee45 | 2020-10-05 17:58:48 +0200 | [diff] [blame] | 89 | * |
| 90 | * If a non-NULL monitor is set for a coroutine, another call |
| 91 | * resetting it to NULL is required before the coroutine terminates, |
| 92 | * otherwise a stale entry would remain in the hash table. |
Kevin Wolf | 947e474 | 2020-10-05 17:58:44 +0200 | [diff] [blame] | 93 | */ |
Kevin Wolf | e69ee45 | 2020-10-05 17:58:48 +0200 | [diff] [blame] | 94 | Monitor *monitor_set_cur(Coroutine *co, Monitor *mon) |
Kevin Wolf | 947e474 | 2020-10-05 17:58:44 +0200 | [diff] [blame] | 95 | { |
Kevin Wolf | e69ee45 | 2020-10-05 17:58:48 +0200 | [diff] [blame] | 96 | Monitor *old_monitor = monitor_cur(); |
Kevin Wolf | 947e474 | 2020-10-05 17:58:44 +0200 | [diff] [blame] | 97 | |
Kevin Wolf | e69ee45 | 2020-10-05 17:58:48 +0200 | [diff] [blame] | 98 | qemu_mutex_lock(&monitor_lock); |
| 99 | if (mon) { |
| 100 | g_hash_table_replace(coroutine_mon, co, mon); |
| 101 | } else { |
| 102 | g_hash_table_remove(coroutine_mon, co); |
| 103 | } |
| 104 | qemu_mutex_unlock(&monitor_lock); |
| 105 | |
Kevin Wolf | 947e474 | 2020-10-05 17:58:44 +0200 | [diff] [blame] | 106 | return old_monitor; |
| 107 | } |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 108 | |
| 109 | /** |
| 110 | * Is the current monitor, if any, a QMP monitor? |
| 111 | */ |
| 112 | bool monitor_cur_is_qmp(void) |
| 113 | { |
Kevin Wolf | 947e474 | 2020-10-05 17:58:44 +0200 | [diff] [blame] | 114 | Monitor *cur_mon = monitor_cur(); |
| 115 | |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 116 | return cur_mon && monitor_is_qmp(cur_mon); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Is @mon is using readline? |
| 121 | * Note: not all HMP monitors use readline, e.g., gdbserver has a |
| 122 | * non-interactive HMP monitor, so readline is not used there. |
| 123 | */ |
Kevin Wolf | 9208241 | 2019-06-13 17:34:03 +0200 | [diff] [blame] | 124 | static inline bool monitor_uses_readline(const MonitorHMP *mon) |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 125 | { |
Kevin Wolf | 9208241 | 2019-06-13 17:34:03 +0200 | [diff] [blame] | 126 | return mon->use_readline; |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | static inline bool monitor_is_hmp_non_interactive(const Monitor *mon) |
| 130 | { |
Kevin Wolf | 9208241 | 2019-06-13 17:34:03 +0200 | [diff] [blame] | 131 | if (monitor_is_qmp(mon)) { |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | return !monitor_uses_readline(container_of(mon, MonitorHMP, common)); |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 136 | } |
| 137 | |
Marc-André Lureau | bf7b1ea | 2021-08-04 17:01:14 +0400 | [diff] [blame] | 138 | static gboolean monitor_unblocked(void *do_not_use, GIOCondition cond, |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 139 | void *opaque) |
| 140 | { |
| 141 | Monitor *mon = opaque; |
| 142 | |
Paolo Bonzini | e37548e | 2023-05-17 14:47:55 +0200 | [diff] [blame] | 143 | QEMU_LOCK_GUARD(&mon->mon_lock); |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 144 | mon->out_watch = 0; |
| 145 | monitor_flush_locked(mon); |
Philippe Mathieu-Daudé | 53c7c92 | 2023-07-05 13:22:10 +0200 | [diff] [blame] | 146 | return G_SOURCE_REMOVE; |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /* Caller must hold mon->mon_lock */ |
Paolo Bonzini | 4cb96b9 | 2023-05-17 14:46:49 +0200 | [diff] [blame] | 150 | void monitor_flush_locked(Monitor *mon) |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 151 | { |
| 152 | int rc; |
| 153 | size_t len; |
| 154 | const char *buf; |
| 155 | |
| 156 | if (mon->skip_flush) { |
| 157 | return; |
| 158 | } |
| 159 | |
Markus Armbruster | 20076f4 | 2020-12-11 18:11:34 +0100 | [diff] [blame] | 160 | buf = mon->outbuf->str; |
| 161 | len = mon->outbuf->len; |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 162 | |
| 163 | if (len && !mon->mux_out) { |
| 164 | rc = qemu_chr_fe_write(&mon->chr, (const uint8_t *) buf, len); |
| 165 | if ((rc < 0 && errno != EAGAIN) || (rc == len)) { |
| 166 | /* all flushed or error */ |
Markus Armbruster | 20076f4 | 2020-12-11 18:11:34 +0100 | [diff] [blame] | 167 | g_string_truncate(mon->outbuf, 0); |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 168 | return; |
| 169 | } |
| 170 | if (rc > 0) { |
| 171 | /* partial write */ |
Markus Armbruster | 20076f4 | 2020-12-11 18:11:34 +0100 | [diff] [blame] | 172 | g_string_erase(mon->outbuf, 0, rc); |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 173 | } |
| 174 | if (mon->out_watch == 0) { |
| 175 | mon->out_watch = |
| 176 | qemu_chr_fe_add_watch(&mon->chr, G_IO_OUT | G_IO_HUP, |
| 177 | monitor_unblocked, mon); |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | void monitor_flush(Monitor *mon) |
| 183 | { |
Paolo Bonzini | e37548e | 2023-05-17 14:47:55 +0200 | [diff] [blame] | 184 | QEMU_LOCK_GUARD(&mon->mon_lock); |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 185 | monitor_flush_locked(mon); |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /* flush at every end of line */ |
Paolo Bonzini | 4cb96b9 | 2023-05-17 14:46:49 +0200 | [diff] [blame] | 189 | int monitor_puts_locked(Monitor *mon, const char *str) |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 190 | { |
| 191 | int i; |
| 192 | char c; |
| 193 | |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 194 | for (i = 0; str[i]; i++) { |
| 195 | c = str[i]; |
| 196 | if (c == '\n') { |
Markus Armbruster | 20076f4 | 2020-12-11 18:11:34 +0100 | [diff] [blame] | 197 | g_string_append_c(mon->outbuf, '\r'); |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 198 | } |
Markus Armbruster | 20076f4 | 2020-12-11 18:11:34 +0100 | [diff] [blame] | 199 | g_string_append_c(mon->outbuf, c); |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 200 | if (c == '\n') { |
| 201 | monitor_flush_locked(mon); |
| 202 | } |
| 203 | } |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 204 | |
| 205 | return i; |
| 206 | } |
| 207 | |
Paolo Bonzini | 4cb96b9 | 2023-05-17 14:46:49 +0200 | [diff] [blame] | 208 | int monitor_puts(Monitor *mon, const char *str) |
| 209 | { |
| 210 | QEMU_LOCK_GUARD(&mon->mon_lock); |
| 211 | return monitor_puts_locked(mon, str); |
| 212 | } |
| 213 | |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 214 | int monitor_vprintf(Monitor *mon, const char *fmt, va_list ap) |
| 215 | { |
| 216 | char *buf; |
| 217 | int n; |
| 218 | |
| 219 | if (!mon) { |
| 220 | return -1; |
| 221 | } |
| 222 | |
| 223 | if (monitor_is_qmp(mon)) { |
| 224 | return -1; |
| 225 | } |
| 226 | |
| 227 | buf = g_strdup_vprintf(fmt, ap); |
| 228 | n = monitor_puts(mon, buf); |
| 229 | g_free(buf); |
| 230 | return n; |
| 231 | } |
| 232 | |
| 233 | int monitor_printf(Monitor *mon, const char *fmt, ...) |
| 234 | { |
| 235 | int ret; |
| 236 | |
| 237 | va_list ap; |
| 238 | va_start(ap, fmt); |
| 239 | ret = monitor_vprintf(mon, fmt, ap); |
| 240 | va_end(ap); |
| 241 | return ret; |
| 242 | } |
| 243 | |
Markus Armbruster | dd00d7f | 2023-01-24 13:19:41 +0100 | [diff] [blame] | 244 | void monitor_printc(Monitor *mon, int c) |
| 245 | { |
| 246 | monitor_printf(mon, "'"); |
| 247 | switch(c) { |
| 248 | case '\'': |
| 249 | monitor_printf(mon, "\\'"); |
| 250 | break; |
| 251 | case '\\': |
| 252 | monitor_printf(mon, "\\\\"); |
| 253 | break; |
| 254 | case '\n': |
| 255 | monitor_printf(mon, "\\n"); |
| 256 | break; |
| 257 | case '\r': |
| 258 | monitor_printf(mon, "\\r"); |
| 259 | break; |
| 260 | default: |
| 261 | if (c >= 32 && c <= 126) { |
| 262 | monitor_printf(mon, "%c", c); |
| 263 | } else { |
| 264 | monitor_printf(mon, "\\x%02x", c); |
| 265 | } |
| 266 | break; |
| 267 | } |
| 268 | monitor_printf(mon, "'"); |
| 269 | } |
| 270 | |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 271 | /* |
| 272 | * Print to current monitor if we have one, else to stderr. |
| 273 | */ |
| 274 | int error_vprintf(const char *fmt, va_list ap) |
| 275 | { |
Kevin Wolf | 947e474 | 2020-10-05 17:58:44 +0200 | [diff] [blame] | 276 | Monitor *cur_mon = monitor_cur(); |
| 277 | |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 278 | if (cur_mon && !monitor_cur_is_qmp()) { |
| 279 | return monitor_vprintf(cur_mon, fmt, ap); |
| 280 | } |
| 281 | return vfprintf(stderr, fmt, ap); |
| 282 | } |
| 283 | |
| 284 | int error_vprintf_unless_qmp(const char *fmt, va_list ap) |
| 285 | { |
Kevin Wolf | 947e474 | 2020-10-05 17:58:44 +0200 | [diff] [blame] | 286 | Monitor *cur_mon = monitor_cur(); |
| 287 | |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 288 | if (!cur_mon) { |
| 289 | return vfprintf(stderr, fmt, ap); |
| 290 | } |
| 291 | if (!monitor_cur_is_qmp()) { |
| 292 | return monitor_vprintf(cur_mon, fmt, ap); |
| 293 | } |
| 294 | return -1; |
| 295 | } |
| 296 | |
Marc-André Lureau | 756a98d | 2022-04-20 17:26:13 +0400 | [diff] [blame] | 297 | int error_printf_unless_qmp(const char *fmt, ...) |
| 298 | { |
| 299 | va_list ap; |
| 300 | int ret; |
| 301 | |
| 302 | va_start(ap, fmt); |
| 303 | ret = error_vprintf_unless_qmp(fmt, ap); |
| 304 | va_end(ap); |
| 305 | return ret; |
| 306 | } |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 307 | |
| 308 | static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = { |
| 309 | /* Limit guest-triggerable events to 1 per second */ |
| 310 | [QAPI_EVENT_RTC_CHANGE] = { 1000 * SCALE_MS }, |
Leonid Kaplan | 2155d2d | 2024-10-02 18:18:06 +0300 | [diff] [blame] | 311 | [QAPI_EVENT_BLOCK_IO_ERROR] = { 1000 * SCALE_MS }, |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 312 | [QAPI_EVENT_WATCHDOG] = { 1000 * SCALE_MS }, |
| 313 | [QAPI_EVENT_BALLOON_CHANGE] = { 1000 * SCALE_MS }, |
| 314 | [QAPI_EVENT_QUORUM_REPORT_BAD] = { 1000 * SCALE_MS }, |
| 315 | [QAPI_EVENT_QUORUM_FAILURE] = { 1000 * SCALE_MS }, |
| 316 | [QAPI_EVENT_VSERPORT_CHANGE] = { 1000 * SCALE_MS }, |
David Hildenbrand | 722a3c7 | 2020-06-26 09:22:44 +0200 | [diff] [blame] | 317 | [QAPI_EVENT_MEMORY_DEVICE_SIZE_CHANGE] = { 1000 * SCALE_MS }, |
Maciej S. Szmigiero | 259ebed | 2023-08-23 23:34:14 +0200 | [diff] [blame] | 318 | [QAPI_EVENT_HV_BALLOON_STATUS_REPORT] = { 1000 * SCALE_MS }, |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 319 | }; |
| 320 | |
| 321 | /* |
| 322 | * Return the clock to use for recording an event's time. |
| 323 | * It's QEMU_CLOCK_REALTIME, except for qtests it's |
| 324 | * QEMU_CLOCK_VIRTUAL, to support testing rate limits. |
| 325 | * Beware: result is invalid before configure_accelerator(). |
| 326 | */ |
| 327 | static inline QEMUClockType monitor_get_event_clock(void) |
| 328 | { |
| 329 | return qtest_enabled() ? QEMU_CLOCK_VIRTUAL : QEMU_CLOCK_REALTIME; |
| 330 | } |
| 331 | |
| 332 | /* |
| 333 | * Broadcast an event to all monitors. |
| 334 | * @qdict is the event object. Its member "event" must match @event. |
| 335 | * Caller must hold monitor_lock. |
| 336 | */ |
| 337 | static void monitor_qapi_event_emit(QAPIEvent event, QDict *qdict) |
| 338 | { |
| 339 | Monitor *mon; |
| 340 | MonitorQMP *qmp_mon; |
| 341 | |
| 342 | trace_monitor_protocol_event_emit(event, qdict); |
| 343 | QTAILQ_FOREACH(mon, &mon_list, entry) { |
| 344 | if (!monitor_is_qmp(mon)) { |
| 345 | continue; |
| 346 | } |
| 347 | |
| 348 | qmp_mon = container_of(mon, MonitorQMP, common); |
| 349 | if (qmp_mon->commands != &qmp_cap_negotiation_commands) { |
| 350 | qmp_send_response(qmp_mon, qdict); |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | static void monitor_qapi_event_handler(void *opaque); |
| 356 | |
| 357 | /* |
| 358 | * Queue a new event for emission to Monitor instances, |
| 359 | * applying any rate limiting if required. |
| 360 | */ |
| 361 | static void |
| 362 | monitor_qapi_event_queue_no_reenter(QAPIEvent event, QDict *qdict) |
| 363 | { |
| 364 | MonitorQAPIEventConf *evconf; |
| 365 | MonitorQAPIEventState *evstate; |
| 366 | |
| 367 | assert(event < QAPI_EVENT__MAX); |
| 368 | evconf = &monitor_qapi_event_conf[event]; |
| 369 | trace_monitor_protocol_event_queue(event, qdict, evconf->rate); |
| 370 | |
Mahmoud Mandour | a8e2ab5 | 2021-03-11 05:15:34 +0200 | [diff] [blame] | 371 | QEMU_LOCK_GUARD(&monitor_lock); |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 372 | |
| 373 | if (!evconf->rate) { |
| 374 | /* Unthrottled event */ |
| 375 | monitor_qapi_event_emit(event, qdict); |
| 376 | } else { |
| 377 | QDict *data = qobject_to(QDict, qdict_get(qdict, "data")); |
| 378 | MonitorQAPIEventState key = { .event = event, .data = data }; |
| 379 | |
| 380 | evstate = g_hash_table_lookup(monitor_qapi_event_state, &key); |
| 381 | assert(!evstate || timer_pending(evstate->timer)); |
| 382 | |
| 383 | if (evstate) { |
| 384 | /* |
| 385 | * Timer is pending for (at least) evconf->rate ns after |
| 386 | * last send. Store event for sending when timer fires, |
| 387 | * replacing a prior stored event if any. |
| 388 | */ |
| 389 | qobject_unref(evstate->qdict); |
| 390 | evstate->qdict = qobject_ref(qdict); |
| 391 | } else { |
| 392 | /* |
| 393 | * Last send was (at least) evconf->rate ns ago. |
| 394 | * Send immediately, and arm the timer to call |
| 395 | * monitor_qapi_event_handler() in evconf->rate ns. Any |
| 396 | * events arriving before then will be delayed until then. |
| 397 | */ |
| 398 | int64_t now = qemu_clock_get_ns(monitor_get_event_clock()); |
| 399 | |
| 400 | monitor_qapi_event_emit(event, qdict); |
| 401 | |
| 402 | evstate = g_new(MonitorQAPIEventState, 1); |
| 403 | evstate->event = event; |
| 404 | evstate->data = qobject_ref(data); |
| 405 | evstate->qdict = NULL; |
| 406 | evstate->timer = timer_new_ns(monitor_get_event_clock(), |
| 407 | monitor_qapi_event_handler, |
| 408 | evstate); |
| 409 | g_hash_table_add(monitor_qapi_event_state, evstate); |
| 410 | timer_mod_ns(evstate->timer, now + evconf->rate); |
| 411 | } |
| 412 | } |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | void qapi_event_emit(QAPIEvent event, QDict *qdict) |
| 416 | { |
| 417 | /* |
| 418 | * monitor_qapi_event_queue_no_reenter() is not reentrant: it |
| 419 | * would deadlock on monitor_lock. Work around by queueing |
| 420 | * events in thread-local storage. |
| 421 | * TODO: remove this, make it re-enter safe. |
| 422 | */ |
| 423 | typedef struct MonitorQapiEvent { |
| 424 | QAPIEvent event; |
| 425 | QDict *qdict; |
| 426 | QSIMPLEQ_ENTRY(MonitorQapiEvent) entry; |
| 427 | } MonitorQapiEvent; |
| 428 | static __thread QSIMPLEQ_HEAD(, MonitorQapiEvent) event_queue; |
| 429 | static __thread bool reentered; |
| 430 | MonitorQapiEvent *ev; |
| 431 | |
| 432 | if (!reentered) { |
| 433 | QSIMPLEQ_INIT(&event_queue); |
| 434 | } |
| 435 | |
| 436 | ev = g_new(MonitorQapiEvent, 1); |
| 437 | ev->qdict = qobject_ref(qdict); |
| 438 | ev->event = event; |
| 439 | QSIMPLEQ_INSERT_TAIL(&event_queue, ev, entry); |
| 440 | if (reentered) { |
| 441 | return; |
| 442 | } |
| 443 | |
| 444 | reentered = true; |
| 445 | |
| 446 | while ((ev = QSIMPLEQ_FIRST(&event_queue)) != NULL) { |
| 447 | QSIMPLEQ_REMOVE_HEAD(&event_queue, entry); |
| 448 | monitor_qapi_event_queue_no_reenter(ev->event, ev->qdict); |
| 449 | qobject_unref(ev->qdict); |
| 450 | g_free(ev); |
| 451 | } |
| 452 | |
| 453 | reentered = false; |
| 454 | } |
| 455 | |
| 456 | /* |
| 457 | * This function runs evconf->rate ns after sending a throttled |
| 458 | * event. |
| 459 | * If another event has since been stored, send it. |
| 460 | */ |
| 461 | static void monitor_qapi_event_handler(void *opaque) |
| 462 | { |
| 463 | MonitorQAPIEventState *evstate = opaque; |
| 464 | MonitorQAPIEventConf *evconf = &monitor_qapi_event_conf[evstate->event]; |
| 465 | |
| 466 | trace_monitor_protocol_event_handler(evstate->event, evstate->qdict); |
Mahmoud Mandour | a8e2ab5 | 2021-03-11 05:15:34 +0200 | [diff] [blame] | 467 | QEMU_LOCK_GUARD(&monitor_lock); |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 468 | |
| 469 | if (evstate->qdict) { |
| 470 | int64_t now = qemu_clock_get_ns(monitor_get_event_clock()); |
| 471 | |
| 472 | monitor_qapi_event_emit(evstate->event, evstate->qdict); |
| 473 | qobject_unref(evstate->qdict); |
| 474 | evstate->qdict = NULL; |
| 475 | timer_mod_ns(evstate->timer, now + evconf->rate); |
| 476 | } else { |
| 477 | g_hash_table_remove(monitor_qapi_event_state, evstate); |
| 478 | qobject_unref(evstate->data); |
| 479 | timer_free(evstate->timer); |
| 480 | g_free(evstate); |
| 481 | } |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | static unsigned int qapi_event_throttle_hash(const void *key) |
| 485 | { |
| 486 | const MonitorQAPIEventState *evstate = key; |
| 487 | unsigned int hash = evstate->event * 255; |
| 488 | |
| 489 | if (evstate->event == QAPI_EVENT_VSERPORT_CHANGE) { |
| 490 | hash += g_str_hash(qdict_get_str(evstate->data, "id")); |
| 491 | } |
| 492 | |
| 493 | if (evstate->event == QAPI_EVENT_QUORUM_REPORT_BAD) { |
| 494 | hash += g_str_hash(qdict_get_str(evstate->data, "node-name")); |
| 495 | } |
| 496 | |
Leonid Kaplan | 2155d2d | 2024-10-02 18:18:06 +0300 | [diff] [blame] | 497 | if (evstate->event == QAPI_EVENT_MEMORY_DEVICE_SIZE_CHANGE || |
| 498 | evstate->event == QAPI_EVENT_BLOCK_IO_ERROR) { |
David Hildenbrand | 77ae230 | 2021-09-29 18:24:45 +0200 | [diff] [blame] | 499 | hash += g_str_hash(qdict_get_str(evstate->data, "qom-path")); |
| 500 | } |
| 501 | |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 502 | return hash; |
| 503 | } |
| 504 | |
| 505 | static gboolean qapi_event_throttle_equal(const void *a, const void *b) |
| 506 | { |
| 507 | const MonitorQAPIEventState *eva = a; |
| 508 | const MonitorQAPIEventState *evb = b; |
| 509 | |
| 510 | if (eva->event != evb->event) { |
| 511 | return FALSE; |
| 512 | } |
| 513 | |
| 514 | if (eva->event == QAPI_EVENT_VSERPORT_CHANGE) { |
| 515 | return !strcmp(qdict_get_str(eva->data, "id"), |
| 516 | qdict_get_str(evb->data, "id")); |
| 517 | } |
| 518 | |
| 519 | if (eva->event == QAPI_EVENT_QUORUM_REPORT_BAD) { |
| 520 | return !strcmp(qdict_get_str(eva->data, "node-name"), |
| 521 | qdict_get_str(evb->data, "node-name")); |
| 522 | } |
| 523 | |
Leonid Kaplan | 2155d2d | 2024-10-02 18:18:06 +0300 | [diff] [blame] | 524 | if (eva->event == QAPI_EVENT_MEMORY_DEVICE_SIZE_CHANGE || |
| 525 | eva->event == QAPI_EVENT_BLOCK_IO_ERROR) { |
David Hildenbrand | 77ae230 | 2021-09-29 18:24:45 +0200 | [diff] [blame] | 526 | return !strcmp(qdict_get_str(eva->data, "qom-path"), |
| 527 | qdict_get_str(evb->data, "qom-path")); |
| 528 | } |
| 529 | |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 530 | return TRUE; |
| 531 | } |
| 532 | |
| 533 | int monitor_suspend(Monitor *mon) |
| 534 | { |
| 535 | if (monitor_is_hmp_non_interactive(mon)) { |
| 536 | return -ENOTTY; |
| 537 | } |
| 538 | |
Stefan Hajnoczi | d73415a | 2020-09-23 11:56:46 +0100 | [diff] [blame] | 539 | qatomic_inc(&mon->suspend_cnt); |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 540 | |
| 541 | if (mon->use_io_thread) { |
| 542 | /* |
| 543 | * Kick I/O thread to make sure this takes effect. It'll be |
| 544 | * evaluated again in prepare() of the watch object. |
| 545 | */ |
| 546 | aio_notify(iothread_get_aio_context(mon_iothread)); |
| 547 | } |
| 548 | |
| 549 | trace_monitor_suspend(mon, 1); |
| 550 | return 0; |
| 551 | } |
| 552 | |
| 553 | static void monitor_accept_input(void *opaque) |
| 554 | { |
| 555 | Monitor *mon = opaque; |
| 556 | |
Paolo Bonzini | 6ee7c82 | 2023-03-03 13:32:13 +0100 | [diff] [blame] | 557 | qemu_mutex_lock(&mon->mon_lock); |
| 558 | if (!monitor_is_qmp(mon) && mon->reset_seen) { |
Paolo Bonzini | c5d0c55 | 2023-05-17 17:19:03 +0200 | [diff] [blame] | 559 | MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common); |
| 560 | assert(hmp_mon->rs); |
Paolo Bonzini | 6ee7c82 | 2023-03-03 13:32:13 +0100 | [diff] [blame] | 561 | readline_restart(hmp_mon->rs); |
| 562 | qemu_mutex_unlock(&mon->mon_lock); |
Paolo Bonzini | c5d0c55 | 2023-05-17 17:19:03 +0200 | [diff] [blame] | 563 | readline_show_prompt(hmp_mon->rs); |
Paolo Bonzini | 6ee7c82 | 2023-03-03 13:32:13 +0100 | [diff] [blame] | 564 | } else { |
| 565 | qemu_mutex_unlock(&mon->mon_lock); |
Paolo Bonzini | c5d0c55 | 2023-05-17 17:19:03 +0200 | [diff] [blame] | 566 | } |
| 567 | |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 568 | qemu_chr_fe_accept_input(&mon->chr); |
| 569 | } |
| 570 | |
| 571 | void monitor_resume(Monitor *mon) |
| 572 | { |
| 573 | if (monitor_is_hmp_non_interactive(mon)) { |
| 574 | return; |
| 575 | } |
| 576 | |
Stefan Hajnoczi | d73415a | 2020-09-23 11:56:46 +0100 | [diff] [blame] | 577 | if (qatomic_dec_fetch(&mon->suspend_cnt) == 0) { |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 578 | AioContext *ctx; |
| 579 | |
| 580 | if (mon->use_io_thread) { |
| 581 | ctx = iothread_get_aio_context(mon_iothread); |
| 582 | } else { |
| 583 | ctx = qemu_get_aio_context(); |
| 584 | } |
| 585 | |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 586 | aio_bh_schedule_oneshot(ctx, monitor_accept_input, mon); |
| 587 | } |
| 588 | |
| 589 | trace_monitor_suspend(mon, -1); |
| 590 | } |
| 591 | |
| 592 | int monitor_can_read(void *opaque) |
| 593 | { |
| 594 | Monitor *mon = opaque; |
| 595 | |
Paolo Bonzini | 6ee7c82 | 2023-03-03 13:32:13 +0100 | [diff] [blame] | 596 | return !qatomic_read(&mon->suspend_cnt); |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | void monitor_list_append(Monitor *mon) |
| 600 | { |
| 601 | qemu_mutex_lock(&monitor_lock); |
| 602 | /* |
| 603 | * This prevents inserting new monitors during monitor_cleanup(). |
| 604 | * A cleaner solution would involve the main thread telling other |
| 605 | * threads to terminate, waiting for their termination. |
| 606 | */ |
| 607 | if (!monitor_destroyed) { |
| 608 | QTAILQ_INSERT_HEAD(&mon_list, mon, entry); |
| 609 | mon = NULL; |
| 610 | } |
| 611 | qemu_mutex_unlock(&monitor_lock); |
| 612 | |
| 613 | if (mon) { |
| 614 | monitor_data_destroy(mon); |
| 615 | g_free(mon); |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | static void monitor_iothread_init(void) |
| 620 | { |
| 621 | mon_iothread = iothread_create("mon_iothread", &error_abort); |
| 622 | } |
| 623 | |
Kevin Wolf | 9208241 | 2019-06-13 17:34:03 +0200 | [diff] [blame] | 624 | void monitor_data_init(Monitor *mon, bool is_qmp, bool skip_flush, |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 625 | bool use_io_thread) |
| 626 | { |
| 627 | if (use_io_thread && !mon_iothread) { |
| 628 | monitor_iothread_init(); |
| 629 | } |
| 630 | qemu_mutex_init(&mon->mon_lock); |
Kevin Wolf | 9208241 | 2019-06-13 17:34:03 +0200 | [diff] [blame] | 631 | mon->is_qmp = is_qmp; |
Markus Armbruster | 20076f4 | 2020-12-11 18:11:34 +0100 | [diff] [blame] | 632 | mon->outbuf = g_string_new(NULL); |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 633 | mon->skip_flush = skip_flush; |
| 634 | mon->use_io_thread = use_io_thread; |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | void monitor_data_destroy(Monitor *mon) |
| 638 | { |
| 639 | g_free(mon->mon_cpu_path); |
| 640 | qemu_chr_fe_deinit(&mon->chr, false); |
| 641 | if (monitor_is_qmp(mon)) { |
| 642 | monitor_data_destroy_qmp(container_of(mon, MonitorQMP, common)); |
| 643 | } else { |
| 644 | readline_free(container_of(mon, MonitorHMP, common)->rs); |
| 645 | } |
Markus Armbruster | 20076f4 | 2020-12-11 18:11:34 +0100 | [diff] [blame] | 646 | g_string_free(mon->outbuf, true); |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 647 | qemu_mutex_destroy(&mon->mon_lock); |
| 648 | } |
| 649 | |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 650 | void monitor_cleanup(void) |
| 651 | { |
| 652 | /* |
Kevin Wolf | 357bda9 | 2020-10-13 14:50:27 +0200 | [diff] [blame] | 653 | * The dispatcher needs to stop before destroying the monitor and |
| 654 | * the I/O thread. |
Kevin Wolf | 9ce44e2 | 2020-10-05 17:58:50 +0200 | [diff] [blame] | 655 | * |
| 656 | * We need to poll both qemu_aio_context and iohandler_ctx to make |
| 657 | * sure that the dispatcher coroutine keeps making progress and |
| 658 | * eventually terminates. qemu_aio_context is automatically |
Stefan Hajnoczi | 9612aa4 | 2023-03-09 14:08:55 -0500 | [diff] [blame] | 659 | * polled by calling AIO_WAIT_WHILE_UNLOCKED on it, but we must poll |
Kevin Wolf | 9ce44e2 | 2020-10-05 17:58:50 +0200 | [diff] [blame] | 660 | * iohandler_ctx manually. |
Kevin Wolf | c81219a | 2021-02-12 18:20:27 +0100 | [diff] [blame] | 661 | * |
| 662 | * Letting the iothread continue while shutting down the dispatcher |
| 663 | * means that new requests may still be coming in. This is okay, |
| 664 | * we'll just leave them in the queue without sending a response |
| 665 | * and monitor_data_destroy() will free them. |
Kevin Wolf | 9ce44e2 | 2020-10-05 17:58:50 +0200 | [diff] [blame] | 666 | */ |
Paolo Bonzini | 0ff2553 | 2023-03-03 12:51:33 +0100 | [diff] [blame] | 667 | WITH_QEMU_LOCK_GUARD(&monitor_lock) { |
| 668 | qmp_dispatcher_co_shutdown = true; |
| 669 | } |
Paolo Bonzini | 9f2d585 | 2023-03-03 13:51:44 +0100 | [diff] [blame] | 670 | qmp_dispatcher_co_wake(); |
Kevin Wolf | 9ce44e2 | 2020-10-05 17:58:50 +0200 | [diff] [blame] | 671 | |
Stefan Hajnoczi | 9612aa4 | 2023-03-09 14:08:55 -0500 | [diff] [blame] | 672 | AIO_WAIT_WHILE_UNLOCKED(NULL, |
Kevin Wolf | 9ce44e2 | 2020-10-05 17:58:50 +0200 | [diff] [blame] | 673 | (aio_poll(iohandler_get_aio_context(), false), |
Paolo Bonzini | 3e6bed6 | 2023-03-03 12:45:29 +0100 | [diff] [blame] | 674 | qatomic_read(&qmp_dispatcher_co))); |
Kevin Wolf | 9ce44e2 | 2020-10-05 17:58:50 +0200 | [diff] [blame] | 675 | |
Kevin Wolf | c81219a | 2021-02-12 18:20:27 +0100 | [diff] [blame] | 676 | /* |
| 677 | * We need to explicitly stop the I/O thread (but not destroy it), |
| 678 | * clean up the monitor resources, then destroy the I/O thread since |
| 679 | * we need to unregister from chardev below in |
| 680 | * monitor_data_destroy(), and chardev is not thread-safe yet |
| 681 | */ |
| 682 | if (mon_iothread) { |
| 683 | iothread_stop(mon_iothread); |
| 684 | } |
| 685 | |
Kevin Wolf | 357bda9 | 2020-10-13 14:50:27 +0200 | [diff] [blame] | 686 | /* Flush output buffers and destroy monitors */ |
| 687 | qemu_mutex_lock(&monitor_lock); |
| 688 | monitor_destroyed = true; |
| 689 | while (!QTAILQ_EMPTY(&mon_list)) { |
| 690 | Monitor *mon = QTAILQ_FIRST(&mon_list); |
| 691 | QTAILQ_REMOVE(&mon_list, mon, entry); |
| 692 | /* Permit QAPI event emission from character frontend release */ |
| 693 | qemu_mutex_unlock(&monitor_lock); |
| 694 | monitor_flush(mon); |
| 695 | monitor_data_destroy(mon); |
| 696 | qemu_mutex_lock(&monitor_lock); |
| 697 | g_free(mon); |
| 698 | } |
| 699 | qemu_mutex_unlock(&monitor_lock); |
| 700 | |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 701 | if (mon_iothread) { |
| 702 | iothread_destroy(mon_iothread); |
| 703 | mon_iothread = NULL; |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | static void monitor_qapi_event_init(void) |
| 708 | { |
| 709 | monitor_qapi_event_state = g_hash_table_new(qapi_event_throttle_hash, |
| 710 | qapi_event_throttle_equal); |
| 711 | } |
| 712 | |
Markus Armbruster | 9d2b5f2 | 2023-01-24 13:19:45 +0100 | [diff] [blame] | 713 | void monitor_init_globals(void) |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 714 | { |
| 715 | monitor_qapi_event_init(); |
| 716 | qemu_mutex_init(&monitor_lock); |
Kevin Wolf | e69ee45 | 2020-10-05 17:58:48 +0200 | [diff] [blame] | 717 | coroutine_mon = g_hash_table_new(NULL, NULL); |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 718 | |
| 719 | /* |
| 720 | * The dispatcher BH must run in the main loop thread, since we |
| 721 | * have commands assuming that context. It would be nice to get |
| 722 | * rid of those assumptions. |
| 723 | */ |
Kevin Wolf | 9ce44e2 | 2020-10-05 17:58:50 +0200 | [diff] [blame] | 724 | qmp_dispatcher_co = qemu_coroutine_create(monitor_qmp_dispatcher_co, NULL); |
Kevin Wolf | 9ce44e2 | 2020-10-05 17:58:50 +0200 | [diff] [blame] | 725 | aio_co_schedule(iohandler_get_aio_context(), qmp_dispatcher_co); |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 726 | } |
| 727 | |
Kevin Wolf | a2f411c | 2020-02-24 15:30:07 +0100 | [diff] [blame] | 728 | int monitor_init(MonitorOptions *opts, bool allow_hmp, Error **errp) |
Kevin Wolf | c3e9555 | 2020-01-29 11:22:36 +0100 | [diff] [blame] | 729 | { |
Markus Armbruster | 50707b3 | 2022-11-21 09:50:49 +0100 | [diff] [blame] | 730 | ERRP_GUARD(); |
Kevin Wolf | c3e9555 | 2020-01-29 11:22:36 +0100 | [diff] [blame] | 731 | Chardev *chr; |
Kevin Wolf | c3e9555 | 2020-01-29 11:22:36 +0100 | [diff] [blame] | 732 | |
Kevin Wolf | f209872 | 2020-02-24 15:30:04 +0100 | [diff] [blame] | 733 | chr = qemu_chr_find(opts->chardev); |
Kevin Wolf | c3e9555 | 2020-01-29 11:22:36 +0100 | [diff] [blame] | 734 | if (chr == NULL) { |
Kevin Wolf | f209872 | 2020-02-24 15:30:04 +0100 | [diff] [blame] | 735 | error_setg(errp, "chardev \"%s\" not found", opts->chardev); |
Kevin Wolf | c3e9555 | 2020-01-29 11:22:36 +0100 | [diff] [blame] | 736 | return -1; |
| 737 | } |
| 738 | |
Kevin Wolf | a2f411c | 2020-02-24 15:30:07 +0100 | [diff] [blame] | 739 | if (!opts->has_mode) { |
| 740 | opts->mode = allow_hmp ? MONITOR_MODE_READLINE : MONITOR_MODE_CONTROL; |
| 741 | } |
| 742 | |
Kevin Wolf | f209872 | 2020-02-24 15:30:04 +0100 | [diff] [blame] | 743 | switch (opts->mode) { |
| 744 | case MONITOR_MODE_CONTROL: |
Markus Armbruster | 50707b3 | 2022-11-21 09:50:49 +0100 | [diff] [blame] | 745 | monitor_init_qmp(chr, opts->pretty, errp); |
Kevin Wolf | f209872 | 2020-02-24 15:30:04 +0100 | [diff] [blame] | 746 | break; |
| 747 | case MONITOR_MODE_READLINE: |
Kevin Wolf | a2f411c | 2020-02-24 15:30:07 +0100 | [diff] [blame] | 748 | if (!allow_hmp) { |
| 749 | error_setg(errp, "Only QMP is supported"); |
| 750 | return -1; |
| 751 | } |
Kevin Wolf | f209872 | 2020-02-24 15:30:04 +0100 | [diff] [blame] | 752 | if (opts->pretty) { |
Daniel P. Berrangé | 283d845 | 2021-02-19 17:56:13 +0000 | [diff] [blame] | 753 | error_setg(errp, "'pretty' is not compatible with HMP monitors"); |
| 754 | return -1; |
Kevin Wolf | f209872 | 2020-02-24 15:30:04 +0100 | [diff] [blame] | 755 | } |
Markus Armbruster | 50707b3 | 2022-11-21 09:50:49 +0100 | [diff] [blame] | 756 | monitor_init_hmp(chr, true, errp); |
Kevin Wolf | f209872 | 2020-02-24 15:30:04 +0100 | [diff] [blame] | 757 | break; |
| 758 | default: |
| 759 | g_assert_not_reached(); |
| 760 | } |
| 761 | |
Markus Armbruster | 50707b3 | 2022-11-21 09:50:49 +0100 | [diff] [blame] | 762 | return *errp ? -1 : 0; |
Kevin Wolf | f209872 | 2020-02-24 15:30:04 +0100 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | int monitor_init_opts(QemuOpts *opts, Error **errp) |
| 766 | { |
| 767 | Visitor *v; |
| 768 | MonitorOptions *options; |
Markus Armbruster | b11a093 | 2020-07-07 18:06:07 +0200 | [diff] [blame] | 769 | int ret; |
Kevin Wolf | f209872 | 2020-02-24 15:30:04 +0100 | [diff] [blame] | 770 | |
| 771 | v = opts_visitor_new(opts); |
Markus Armbruster | b11a093 | 2020-07-07 18:06:07 +0200 | [diff] [blame] | 772 | visit_type_MonitorOptions(v, NULL, &options, errp); |
Kevin Wolf | f209872 | 2020-02-24 15:30:04 +0100 | [diff] [blame] | 773 | visit_free(v); |
Markus Armbruster | b11a093 | 2020-07-07 18:06:07 +0200 | [diff] [blame] | 774 | if (!options) { |
Kevin Wolf | f209872 | 2020-02-24 15:30:04 +0100 | [diff] [blame] | 775 | return -1; |
Kevin Wolf | c3e9555 | 2020-01-29 11:22:36 +0100 | [diff] [blame] | 776 | } |
Markus Armbruster | b11a093 | 2020-07-07 18:06:07 +0200 | [diff] [blame] | 777 | |
| 778 | ret = monitor_init(options, true, errp); |
| 779 | qapi_free_MonitorOptions(options); |
| 780 | return ret; |
Kevin Wolf | c3e9555 | 2020-01-29 11:22:36 +0100 | [diff] [blame] | 781 | } |
| 782 | |
Kevin Wolf | 1d95db7 | 2019-06-13 17:34:02 +0200 | [diff] [blame] | 783 | QemuOptsList qemu_mon_opts = { |
| 784 | .name = "mon", |
| 785 | .implied_opt_name = "chardev", |
| 786 | .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head), |
| 787 | .desc = { |
| 788 | { |
| 789 | .name = "mode", |
| 790 | .type = QEMU_OPT_STRING, |
| 791 | },{ |
| 792 | .name = "chardev", |
| 793 | .type = QEMU_OPT_STRING, |
| 794 | },{ |
| 795 | .name = "pretty", |
| 796 | .type = QEMU_OPT_BOOL, |
| 797 | }, |
| 798 | { /* end of list */ } |
| 799 | }, |
| 800 | }; |