11d95db74SKevin Wolf /* 21d95db74SKevin Wolf * QEMU monitor 31d95db74SKevin Wolf * 41d95db74SKevin Wolf * Copyright (c) 2003-2004 Fabrice Bellard 51d95db74SKevin Wolf * 61d95db74SKevin Wolf * Permission is hereby granted, free of charge, to any person obtaining a copy 71d95db74SKevin Wolf * of this software and associated documentation files (the "Software"), to deal 81d95db74SKevin Wolf * in the Software without restriction, including without limitation the rights 91d95db74SKevin Wolf * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 101d95db74SKevin Wolf * copies of the Software, and to permit persons to whom the Software is 111d95db74SKevin Wolf * furnished to do so, subject to the following conditions: 121d95db74SKevin Wolf * 131d95db74SKevin Wolf * The above copyright notice and this permission notice shall be included in 141d95db74SKevin Wolf * all copies or substantial portions of the Software. 151d95db74SKevin Wolf * 161d95db74SKevin Wolf * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 171d95db74SKevin Wolf * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 181d95db74SKevin Wolf * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 191d95db74SKevin Wolf * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 201d95db74SKevin Wolf * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 211d95db74SKevin Wolf * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 221d95db74SKevin Wolf * THE SOFTWARE. 231d95db74SKevin Wolf */ 241d95db74SKevin Wolf 251d95db74SKevin Wolf #include "qemu/osdep.h" 261d95db74SKevin Wolf #include "monitor-internal.h" 271d95db74SKevin Wolf #include "qapi/error.h" 28f2098725SKevin Wolf #include "qapi/opts-visitor.h" 291d95db74SKevin Wolf #include "qapi/qapi-emit-events.h" 30f2098725SKevin Wolf #include "qapi/qapi-visit-control.h" 311d95db74SKevin Wolf #include "qapi/qmp/qdict.h" 321d95db74SKevin Wolf #include "qemu/error-report.h" 331d95db74SKevin Wolf #include "qemu/option.h" 341d95db74SKevin Wolf #include "sysemu/qtest.h" 351d95db74SKevin Wolf #include "trace.h" 361d95db74SKevin Wolf 371d95db74SKevin Wolf /* 381d95db74SKevin Wolf * To prevent flooding clients, events can be throttled. The 391d95db74SKevin Wolf * throttling is calculated globally, rather than per-Monitor 401d95db74SKevin Wolf * instance. 411d95db74SKevin Wolf */ 421d95db74SKevin Wolf typedef struct MonitorQAPIEventState { 431d95db74SKevin Wolf QAPIEvent event; /* Throttling state for this event type and... */ 441d95db74SKevin Wolf QDict *data; /* ... data, see qapi_event_throttle_equal() */ 451d95db74SKevin Wolf QEMUTimer *timer; /* Timer for handling delayed events */ 461d95db74SKevin Wolf QDict *qdict; /* Delayed event (if any) */ 471d95db74SKevin Wolf } MonitorQAPIEventState; 481d95db74SKevin Wolf 491d95db74SKevin Wolf typedef struct { 501d95db74SKevin Wolf int64_t rate; /* Minimum time (in ns) between two events */ 511d95db74SKevin Wolf } MonitorQAPIEventConf; 521d95db74SKevin Wolf 531d95db74SKevin Wolf /* Shared monitor I/O thread */ 541d95db74SKevin Wolf IOThread *mon_iothread; 551d95db74SKevin Wolf 569ce44e2cSKevin Wolf /* Coroutine to dispatch the requests received from I/O thread */ 579ce44e2cSKevin Wolf Coroutine *qmp_dispatcher_co; 589ce44e2cSKevin Wolf 599ce44e2cSKevin Wolf /* Set to true when the dispatcher coroutine should terminate */ 609ce44e2cSKevin Wolf bool qmp_dispatcher_co_shutdown; 619ce44e2cSKevin Wolf 629ce44e2cSKevin Wolf /* 639ce44e2cSKevin Wolf * qmp_dispatcher_co_busy is used for synchronisation between the 649ce44e2cSKevin Wolf * monitor thread and the main thread to ensure that the dispatcher 659ce44e2cSKevin Wolf * coroutine never gets scheduled a second time when it's already 669ce44e2cSKevin Wolf * scheduled (scheduling the same coroutine twice is forbidden). 679ce44e2cSKevin Wolf * 689ce44e2cSKevin Wolf * It is true if the coroutine is active and processing requests. 699ce44e2cSKevin Wolf * Additional requests may then be pushed onto mon->qmp_requests, 709ce44e2cSKevin Wolf * and @qmp_dispatcher_co_shutdown may be set without further ado. 719ce44e2cSKevin Wolf * @qmp_dispatcher_co_busy must not be woken up in this case. 729ce44e2cSKevin Wolf * 739ce44e2cSKevin Wolf * If false, you also have to set @qmp_dispatcher_co_busy to true and 749ce44e2cSKevin Wolf * wake up @qmp_dispatcher_co after pushing the new requests. 759ce44e2cSKevin Wolf * 769ce44e2cSKevin Wolf * The coroutine will automatically change this variable back to false 779ce44e2cSKevin Wolf * before it yields. Nobody else may set the variable to false. 789ce44e2cSKevin Wolf * 799ce44e2cSKevin Wolf * Access must be atomic for thread safety. 809ce44e2cSKevin Wolf */ 819ce44e2cSKevin Wolf bool qmp_dispatcher_co_busy; 821d95db74SKevin Wolf 83e69ee454SKevin Wolf /* 84e69ee454SKevin Wolf * Protects mon_list, monitor_qapi_event_state, coroutine_mon, 85e69ee454SKevin Wolf * monitor_destroyed. 86e69ee454SKevin Wolf */ 871d95db74SKevin Wolf QemuMutex monitor_lock; 881d95db74SKevin Wolf static GHashTable *monitor_qapi_event_state; 89e69ee454SKevin Wolf static GHashTable *coroutine_mon; /* Maps Coroutine* to Monitor* */ 901d95db74SKevin Wolf 911d95db74SKevin Wolf MonitorList mon_list; 921d95db74SKevin Wolf int mon_refcount; 931d95db74SKevin Wolf static bool monitor_destroyed; 941d95db74SKevin Wolf 95947e4744SKevin Wolf Monitor *monitor_cur(void) 96947e4744SKevin Wolf { 97e69ee454SKevin Wolf Monitor *mon; 98e69ee454SKevin Wolf 99e69ee454SKevin Wolf qemu_mutex_lock(&monitor_lock); 100e69ee454SKevin Wolf mon = g_hash_table_lookup(coroutine_mon, qemu_coroutine_self()); 101e69ee454SKevin Wolf qemu_mutex_unlock(&monitor_lock); 102e69ee454SKevin Wolf 103e69ee454SKevin Wolf return mon; 104947e4744SKevin Wolf } 105947e4744SKevin Wolf 106947e4744SKevin Wolf /** 107947e4744SKevin Wolf * Sets a new current monitor and returns the old one. 108e69ee454SKevin Wolf * 109e69ee454SKevin Wolf * If a non-NULL monitor is set for a coroutine, another call 110e69ee454SKevin Wolf * resetting it to NULL is required before the coroutine terminates, 111e69ee454SKevin Wolf * otherwise a stale entry would remain in the hash table. 112947e4744SKevin Wolf */ 113e69ee454SKevin Wolf Monitor *monitor_set_cur(Coroutine *co, Monitor *mon) 114947e4744SKevin Wolf { 115e69ee454SKevin Wolf Monitor *old_monitor = monitor_cur(); 116947e4744SKevin Wolf 117e69ee454SKevin Wolf qemu_mutex_lock(&monitor_lock); 118e69ee454SKevin Wolf if (mon) { 119e69ee454SKevin Wolf g_hash_table_replace(coroutine_mon, co, mon); 120e69ee454SKevin Wolf } else { 121e69ee454SKevin Wolf g_hash_table_remove(coroutine_mon, co); 122e69ee454SKevin Wolf } 123e69ee454SKevin Wolf qemu_mutex_unlock(&monitor_lock); 124e69ee454SKevin Wolf 125947e4744SKevin Wolf return old_monitor; 126947e4744SKevin Wolf } 1271d95db74SKevin Wolf 1281d95db74SKevin Wolf /** 1291d95db74SKevin Wolf * Is the current monitor, if any, a QMP monitor? 1301d95db74SKevin Wolf */ 1311d95db74SKevin Wolf bool monitor_cur_is_qmp(void) 1321d95db74SKevin Wolf { 133947e4744SKevin Wolf Monitor *cur_mon = monitor_cur(); 134947e4744SKevin Wolf 1351d95db74SKevin Wolf return cur_mon && monitor_is_qmp(cur_mon); 1361d95db74SKevin Wolf } 1371d95db74SKevin Wolf 1381d95db74SKevin Wolf /** 1391d95db74SKevin Wolf * Is @mon is using readline? 1401d95db74SKevin Wolf * Note: not all HMP monitors use readline, e.g., gdbserver has a 1411d95db74SKevin Wolf * non-interactive HMP monitor, so readline is not used there. 1421d95db74SKevin Wolf */ 14392082416SKevin Wolf static inline bool monitor_uses_readline(const MonitorHMP *mon) 1441d95db74SKevin Wolf { 14592082416SKevin Wolf return mon->use_readline; 1461d95db74SKevin Wolf } 1471d95db74SKevin Wolf 1481d95db74SKevin Wolf static inline bool monitor_is_hmp_non_interactive(const Monitor *mon) 1491d95db74SKevin Wolf { 15092082416SKevin Wolf if (monitor_is_qmp(mon)) { 15192082416SKevin Wolf return false; 15292082416SKevin Wolf } 15392082416SKevin Wolf 15492082416SKevin Wolf return !monitor_uses_readline(container_of(mon, MonitorHMP, common)); 1551d95db74SKevin Wolf } 1561d95db74SKevin Wolf 157bf7b1eabSMarc-André Lureau static gboolean monitor_unblocked(void *do_not_use, GIOCondition cond, 1581d95db74SKevin Wolf void *opaque) 1591d95db74SKevin Wolf { 1601d95db74SKevin Wolf Monitor *mon = opaque; 1611d95db74SKevin Wolf 162e37548efSPaolo Bonzini QEMU_LOCK_GUARD(&mon->mon_lock); 1631d95db74SKevin Wolf mon->out_watch = 0; 1641d95db74SKevin Wolf monitor_flush_locked(mon); 1651d95db74SKevin Wolf return FALSE; 1661d95db74SKevin Wolf } 1671d95db74SKevin Wolf 1681d95db74SKevin Wolf /* Caller must hold mon->mon_lock */ 1694cb96b97SPaolo Bonzini void monitor_flush_locked(Monitor *mon) 1701d95db74SKevin Wolf { 1711d95db74SKevin Wolf int rc; 1721d95db74SKevin Wolf size_t len; 1731d95db74SKevin Wolf const char *buf; 1741d95db74SKevin Wolf 1751d95db74SKevin Wolf if (mon->skip_flush) { 1761d95db74SKevin Wolf return; 1771d95db74SKevin Wolf } 1781d95db74SKevin Wolf 17920076f4aSMarkus Armbruster buf = mon->outbuf->str; 18020076f4aSMarkus Armbruster len = mon->outbuf->len; 1811d95db74SKevin Wolf 1821d95db74SKevin Wolf if (len && !mon->mux_out) { 1831d95db74SKevin Wolf rc = qemu_chr_fe_write(&mon->chr, (const uint8_t *) buf, len); 1841d95db74SKevin Wolf if ((rc < 0 && errno != EAGAIN) || (rc == len)) { 1851d95db74SKevin Wolf /* all flushed or error */ 18620076f4aSMarkus Armbruster g_string_truncate(mon->outbuf, 0); 1871d95db74SKevin Wolf return; 1881d95db74SKevin Wolf } 1891d95db74SKevin Wolf if (rc > 0) { 1901d95db74SKevin Wolf /* partial write */ 19120076f4aSMarkus Armbruster g_string_erase(mon->outbuf, 0, rc); 1921d95db74SKevin Wolf } 1931d95db74SKevin Wolf if (mon->out_watch == 0) { 1941d95db74SKevin Wolf mon->out_watch = 1951d95db74SKevin Wolf qemu_chr_fe_add_watch(&mon->chr, G_IO_OUT | G_IO_HUP, 1961d95db74SKevin Wolf monitor_unblocked, mon); 1971d95db74SKevin Wolf } 1981d95db74SKevin Wolf } 1991d95db74SKevin Wolf } 2001d95db74SKevin Wolf 2011d95db74SKevin Wolf void monitor_flush(Monitor *mon) 2021d95db74SKevin Wolf { 203e37548efSPaolo Bonzini QEMU_LOCK_GUARD(&mon->mon_lock); 2041d95db74SKevin Wolf monitor_flush_locked(mon); 2051d95db74SKevin Wolf } 2061d95db74SKevin Wolf 2071d95db74SKevin Wolf /* flush at every end of line */ 2084cb96b97SPaolo Bonzini int monitor_puts_locked(Monitor *mon, const char *str) 2091d95db74SKevin Wolf { 2101d95db74SKevin Wolf int i; 2111d95db74SKevin Wolf char c; 2121d95db74SKevin Wolf 2131d95db74SKevin Wolf for (i = 0; str[i]; i++) { 2141d95db74SKevin Wolf c = str[i]; 2151d95db74SKevin Wolf if (c == '\n') { 21620076f4aSMarkus Armbruster g_string_append_c(mon->outbuf, '\r'); 2171d95db74SKevin Wolf } 21820076f4aSMarkus Armbruster g_string_append_c(mon->outbuf, c); 2191d95db74SKevin Wolf if (c == '\n') { 2201d95db74SKevin Wolf monitor_flush_locked(mon); 2211d95db74SKevin Wolf } 2221d95db74SKevin Wolf } 2231d95db74SKevin Wolf 2241d95db74SKevin Wolf return i; 2251d95db74SKevin Wolf } 2261d95db74SKevin Wolf 2274cb96b97SPaolo Bonzini int monitor_puts(Monitor *mon, const char *str) 2284cb96b97SPaolo Bonzini { 2294cb96b97SPaolo Bonzini QEMU_LOCK_GUARD(&mon->mon_lock); 2304cb96b97SPaolo Bonzini return monitor_puts_locked(mon, str); 2314cb96b97SPaolo Bonzini } 2324cb96b97SPaolo Bonzini 2331d95db74SKevin Wolf int monitor_vprintf(Monitor *mon, const char *fmt, va_list ap) 2341d95db74SKevin Wolf { 2351d95db74SKevin Wolf char *buf; 2361d95db74SKevin Wolf int n; 2371d95db74SKevin Wolf 2381d95db74SKevin Wolf if (!mon) { 2391d95db74SKevin Wolf return -1; 2401d95db74SKevin Wolf } 2411d95db74SKevin Wolf 2421d95db74SKevin Wolf if (monitor_is_qmp(mon)) { 2431d95db74SKevin Wolf return -1; 2441d95db74SKevin Wolf } 2451d95db74SKevin Wolf 2461d95db74SKevin Wolf buf = g_strdup_vprintf(fmt, ap); 2471d95db74SKevin Wolf n = monitor_puts(mon, buf); 2481d95db74SKevin Wolf g_free(buf); 2491d95db74SKevin Wolf return n; 2501d95db74SKevin Wolf } 2511d95db74SKevin Wolf 2521d95db74SKevin Wolf int monitor_printf(Monitor *mon, const char *fmt, ...) 2531d95db74SKevin Wolf { 2541d95db74SKevin Wolf int ret; 2551d95db74SKevin Wolf 2561d95db74SKevin Wolf va_list ap; 2571d95db74SKevin Wolf va_start(ap, fmt); 2581d95db74SKevin Wolf ret = monitor_vprintf(mon, fmt, ap); 2591d95db74SKevin Wolf va_end(ap); 2601d95db74SKevin Wolf return ret; 2611d95db74SKevin Wolf } 2621d95db74SKevin Wolf 263dd00d7faSMarkus Armbruster void monitor_printc(Monitor *mon, int c) 264dd00d7faSMarkus Armbruster { 265dd00d7faSMarkus Armbruster monitor_printf(mon, "'"); 266dd00d7faSMarkus Armbruster switch(c) { 267dd00d7faSMarkus Armbruster case '\'': 268dd00d7faSMarkus Armbruster monitor_printf(mon, "\\'"); 269dd00d7faSMarkus Armbruster break; 270dd00d7faSMarkus Armbruster case '\\': 271dd00d7faSMarkus Armbruster monitor_printf(mon, "\\\\"); 272dd00d7faSMarkus Armbruster break; 273dd00d7faSMarkus Armbruster case '\n': 274dd00d7faSMarkus Armbruster monitor_printf(mon, "\\n"); 275dd00d7faSMarkus Armbruster break; 276dd00d7faSMarkus Armbruster case '\r': 277dd00d7faSMarkus Armbruster monitor_printf(mon, "\\r"); 278dd00d7faSMarkus Armbruster break; 279dd00d7faSMarkus Armbruster default: 280dd00d7faSMarkus Armbruster if (c >= 32 && c <= 126) { 281dd00d7faSMarkus Armbruster monitor_printf(mon, "%c", c); 282dd00d7faSMarkus Armbruster } else { 283dd00d7faSMarkus Armbruster monitor_printf(mon, "\\x%02x", c); 284dd00d7faSMarkus Armbruster } 285dd00d7faSMarkus Armbruster break; 286dd00d7faSMarkus Armbruster } 287dd00d7faSMarkus Armbruster monitor_printf(mon, "'"); 288dd00d7faSMarkus Armbruster } 289dd00d7faSMarkus Armbruster 2901d95db74SKevin Wolf /* 2911d95db74SKevin Wolf * Print to current monitor if we have one, else to stderr. 2921d95db74SKevin Wolf */ 2931d95db74SKevin Wolf int error_vprintf(const char *fmt, va_list ap) 2941d95db74SKevin Wolf { 295947e4744SKevin Wolf Monitor *cur_mon = monitor_cur(); 296947e4744SKevin Wolf 2971d95db74SKevin Wolf if (cur_mon && !monitor_cur_is_qmp()) { 2981d95db74SKevin Wolf return monitor_vprintf(cur_mon, fmt, ap); 2991d95db74SKevin Wolf } 3001d95db74SKevin Wolf return vfprintf(stderr, fmt, ap); 3011d95db74SKevin Wolf } 3021d95db74SKevin Wolf 3031d95db74SKevin Wolf int error_vprintf_unless_qmp(const char *fmt, va_list ap) 3041d95db74SKevin Wolf { 305947e4744SKevin Wolf Monitor *cur_mon = monitor_cur(); 306947e4744SKevin Wolf 3071d95db74SKevin Wolf if (!cur_mon) { 3081d95db74SKevin Wolf return vfprintf(stderr, fmt, ap); 3091d95db74SKevin Wolf } 3101d95db74SKevin Wolf if (!monitor_cur_is_qmp()) { 3111d95db74SKevin Wolf return monitor_vprintf(cur_mon, fmt, ap); 3121d95db74SKevin Wolf } 3131d95db74SKevin Wolf return -1; 3141d95db74SKevin Wolf } 3151d95db74SKevin Wolf 316756a98ddSMarc-André Lureau int error_printf_unless_qmp(const char *fmt, ...) 317756a98ddSMarc-André Lureau { 318756a98ddSMarc-André Lureau va_list ap; 319756a98ddSMarc-André Lureau int ret; 320756a98ddSMarc-André Lureau 321756a98ddSMarc-André Lureau va_start(ap, fmt); 322756a98ddSMarc-André Lureau ret = error_vprintf_unless_qmp(fmt, ap); 323756a98ddSMarc-André Lureau va_end(ap); 324756a98ddSMarc-André Lureau return ret; 325756a98ddSMarc-André Lureau } 3261d95db74SKevin Wolf 3271d95db74SKevin Wolf static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = { 3281d95db74SKevin Wolf /* Limit guest-triggerable events to 1 per second */ 3291d95db74SKevin Wolf [QAPI_EVENT_RTC_CHANGE] = { 1000 * SCALE_MS }, 3301d95db74SKevin Wolf [QAPI_EVENT_WATCHDOG] = { 1000 * SCALE_MS }, 3311d95db74SKevin Wolf [QAPI_EVENT_BALLOON_CHANGE] = { 1000 * SCALE_MS }, 3321d95db74SKevin Wolf [QAPI_EVENT_QUORUM_REPORT_BAD] = { 1000 * SCALE_MS }, 3331d95db74SKevin Wolf [QAPI_EVENT_QUORUM_FAILURE] = { 1000 * SCALE_MS }, 3341d95db74SKevin Wolf [QAPI_EVENT_VSERPORT_CHANGE] = { 1000 * SCALE_MS }, 335722a3c78SDavid Hildenbrand [QAPI_EVENT_MEMORY_DEVICE_SIZE_CHANGE] = { 1000 * SCALE_MS }, 3361d95db74SKevin Wolf }; 3371d95db74SKevin Wolf 3381d95db74SKevin Wolf /* 3391d95db74SKevin Wolf * Return the clock to use for recording an event's time. 3401d95db74SKevin Wolf * It's QEMU_CLOCK_REALTIME, except for qtests it's 3411d95db74SKevin Wolf * QEMU_CLOCK_VIRTUAL, to support testing rate limits. 3421d95db74SKevin Wolf * Beware: result is invalid before configure_accelerator(). 3431d95db74SKevin Wolf */ 3441d95db74SKevin Wolf static inline QEMUClockType monitor_get_event_clock(void) 3451d95db74SKevin Wolf { 3461d95db74SKevin Wolf return qtest_enabled() ? QEMU_CLOCK_VIRTUAL : QEMU_CLOCK_REALTIME; 3471d95db74SKevin Wolf } 3481d95db74SKevin Wolf 3491d95db74SKevin Wolf /* 3501d95db74SKevin Wolf * Broadcast an event to all monitors. 3511d95db74SKevin Wolf * @qdict is the event object. Its member "event" must match @event. 3521d95db74SKevin Wolf * Caller must hold monitor_lock. 3531d95db74SKevin Wolf */ 3541d95db74SKevin Wolf static void monitor_qapi_event_emit(QAPIEvent event, QDict *qdict) 3551d95db74SKevin Wolf { 3561d95db74SKevin Wolf Monitor *mon; 3571d95db74SKevin Wolf MonitorQMP *qmp_mon; 3581d95db74SKevin Wolf 3591d95db74SKevin Wolf trace_monitor_protocol_event_emit(event, qdict); 3601d95db74SKevin Wolf QTAILQ_FOREACH(mon, &mon_list, entry) { 3611d95db74SKevin Wolf if (!monitor_is_qmp(mon)) { 3621d95db74SKevin Wolf continue; 3631d95db74SKevin Wolf } 3641d95db74SKevin Wolf 3651d95db74SKevin Wolf qmp_mon = container_of(mon, MonitorQMP, common); 3661d95db74SKevin Wolf if (qmp_mon->commands != &qmp_cap_negotiation_commands) { 3671d95db74SKevin Wolf qmp_send_response(qmp_mon, qdict); 3681d95db74SKevin Wolf } 3691d95db74SKevin Wolf } 3701d95db74SKevin Wolf } 3711d95db74SKevin Wolf 3721d95db74SKevin Wolf static void monitor_qapi_event_handler(void *opaque); 3731d95db74SKevin Wolf 3741d95db74SKevin Wolf /* 3751d95db74SKevin Wolf * Queue a new event for emission to Monitor instances, 3761d95db74SKevin Wolf * applying any rate limiting if required. 3771d95db74SKevin Wolf */ 3781d95db74SKevin Wolf static void 3791d95db74SKevin Wolf monitor_qapi_event_queue_no_reenter(QAPIEvent event, QDict *qdict) 3801d95db74SKevin Wolf { 3811d95db74SKevin Wolf MonitorQAPIEventConf *evconf; 3821d95db74SKevin Wolf MonitorQAPIEventState *evstate; 3831d95db74SKevin Wolf 3841d95db74SKevin Wolf assert(event < QAPI_EVENT__MAX); 3851d95db74SKevin Wolf evconf = &monitor_qapi_event_conf[event]; 3861d95db74SKevin Wolf trace_monitor_protocol_event_queue(event, qdict, evconf->rate); 3871d95db74SKevin Wolf 388a8e2ab5dSMahmoud Mandour QEMU_LOCK_GUARD(&monitor_lock); 3891d95db74SKevin Wolf 3901d95db74SKevin Wolf if (!evconf->rate) { 3911d95db74SKevin Wolf /* Unthrottled event */ 3921d95db74SKevin Wolf monitor_qapi_event_emit(event, qdict); 3931d95db74SKevin Wolf } else { 3941d95db74SKevin Wolf QDict *data = qobject_to(QDict, qdict_get(qdict, "data")); 3951d95db74SKevin Wolf MonitorQAPIEventState key = { .event = event, .data = data }; 3961d95db74SKevin Wolf 3971d95db74SKevin Wolf evstate = g_hash_table_lookup(monitor_qapi_event_state, &key); 3981d95db74SKevin Wolf assert(!evstate || timer_pending(evstate->timer)); 3991d95db74SKevin Wolf 4001d95db74SKevin Wolf if (evstate) { 4011d95db74SKevin Wolf /* 4021d95db74SKevin Wolf * Timer is pending for (at least) evconf->rate ns after 4031d95db74SKevin Wolf * last send. Store event for sending when timer fires, 4041d95db74SKevin Wolf * replacing a prior stored event if any. 4051d95db74SKevin Wolf */ 4061d95db74SKevin Wolf qobject_unref(evstate->qdict); 4071d95db74SKevin Wolf evstate->qdict = qobject_ref(qdict); 4081d95db74SKevin Wolf } else { 4091d95db74SKevin Wolf /* 4101d95db74SKevin Wolf * Last send was (at least) evconf->rate ns ago. 4111d95db74SKevin Wolf * Send immediately, and arm the timer to call 4121d95db74SKevin Wolf * monitor_qapi_event_handler() in evconf->rate ns. Any 4131d95db74SKevin Wolf * events arriving before then will be delayed until then. 4141d95db74SKevin Wolf */ 4151d95db74SKevin Wolf int64_t now = qemu_clock_get_ns(monitor_get_event_clock()); 4161d95db74SKevin Wolf 4171d95db74SKevin Wolf monitor_qapi_event_emit(event, qdict); 4181d95db74SKevin Wolf 4191d95db74SKevin Wolf evstate = g_new(MonitorQAPIEventState, 1); 4201d95db74SKevin Wolf evstate->event = event; 4211d95db74SKevin Wolf evstate->data = qobject_ref(data); 4221d95db74SKevin Wolf evstate->qdict = NULL; 4231d95db74SKevin Wolf evstate->timer = timer_new_ns(monitor_get_event_clock(), 4241d95db74SKevin Wolf monitor_qapi_event_handler, 4251d95db74SKevin Wolf evstate); 4261d95db74SKevin Wolf g_hash_table_add(monitor_qapi_event_state, evstate); 4271d95db74SKevin Wolf timer_mod_ns(evstate->timer, now + evconf->rate); 4281d95db74SKevin Wolf } 4291d95db74SKevin Wolf } 4301d95db74SKevin Wolf } 4311d95db74SKevin Wolf 4321d95db74SKevin Wolf void qapi_event_emit(QAPIEvent event, QDict *qdict) 4331d95db74SKevin Wolf { 4341d95db74SKevin Wolf /* 4351d95db74SKevin Wolf * monitor_qapi_event_queue_no_reenter() is not reentrant: it 4361d95db74SKevin Wolf * would deadlock on monitor_lock. Work around by queueing 4371d95db74SKevin Wolf * events in thread-local storage. 4381d95db74SKevin Wolf * TODO: remove this, make it re-enter safe. 4391d95db74SKevin Wolf */ 4401d95db74SKevin Wolf typedef struct MonitorQapiEvent { 4411d95db74SKevin Wolf QAPIEvent event; 4421d95db74SKevin Wolf QDict *qdict; 4431d95db74SKevin Wolf QSIMPLEQ_ENTRY(MonitorQapiEvent) entry; 4441d95db74SKevin Wolf } MonitorQapiEvent; 4451d95db74SKevin Wolf static __thread QSIMPLEQ_HEAD(, MonitorQapiEvent) event_queue; 4461d95db74SKevin Wolf static __thread bool reentered; 4471d95db74SKevin Wolf MonitorQapiEvent *ev; 4481d95db74SKevin Wolf 4491d95db74SKevin Wolf if (!reentered) { 4501d95db74SKevin Wolf QSIMPLEQ_INIT(&event_queue); 4511d95db74SKevin Wolf } 4521d95db74SKevin Wolf 4531d95db74SKevin Wolf ev = g_new(MonitorQapiEvent, 1); 4541d95db74SKevin Wolf ev->qdict = qobject_ref(qdict); 4551d95db74SKevin Wolf ev->event = event; 4561d95db74SKevin Wolf QSIMPLEQ_INSERT_TAIL(&event_queue, ev, entry); 4571d95db74SKevin Wolf if (reentered) { 4581d95db74SKevin Wolf return; 4591d95db74SKevin Wolf } 4601d95db74SKevin Wolf 4611d95db74SKevin Wolf reentered = true; 4621d95db74SKevin Wolf 4631d95db74SKevin Wolf while ((ev = QSIMPLEQ_FIRST(&event_queue)) != NULL) { 4641d95db74SKevin Wolf QSIMPLEQ_REMOVE_HEAD(&event_queue, entry); 4651d95db74SKevin Wolf monitor_qapi_event_queue_no_reenter(ev->event, ev->qdict); 4661d95db74SKevin Wolf qobject_unref(ev->qdict); 4671d95db74SKevin Wolf g_free(ev); 4681d95db74SKevin Wolf } 4691d95db74SKevin Wolf 4701d95db74SKevin Wolf reentered = false; 4711d95db74SKevin Wolf } 4721d95db74SKevin Wolf 4731d95db74SKevin Wolf /* 4741d95db74SKevin Wolf * This function runs evconf->rate ns after sending a throttled 4751d95db74SKevin Wolf * event. 4761d95db74SKevin Wolf * If another event has since been stored, send it. 4771d95db74SKevin Wolf */ 4781d95db74SKevin Wolf static void monitor_qapi_event_handler(void *opaque) 4791d95db74SKevin Wolf { 4801d95db74SKevin Wolf MonitorQAPIEventState *evstate = opaque; 4811d95db74SKevin Wolf MonitorQAPIEventConf *evconf = &monitor_qapi_event_conf[evstate->event]; 4821d95db74SKevin Wolf 4831d95db74SKevin Wolf trace_monitor_protocol_event_handler(evstate->event, evstate->qdict); 484a8e2ab5dSMahmoud Mandour QEMU_LOCK_GUARD(&monitor_lock); 4851d95db74SKevin Wolf 4861d95db74SKevin Wolf if (evstate->qdict) { 4871d95db74SKevin Wolf int64_t now = qemu_clock_get_ns(monitor_get_event_clock()); 4881d95db74SKevin Wolf 4891d95db74SKevin Wolf monitor_qapi_event_emit(evstate->event, evstate->qdict); 4901d95db74SKevin Wolf qobject_unref(evstate->qdict); 4911d95db74SKevin Wolf evstate->qdict = NULL; 4921d95db74SKevin Wolf timer_mod_ns(evstate->timer, now + evconf->rate); 4931d95db74SKevin Wolf } else { 4941d95db74SKevin Wolf g_hash_table_remove(monitor_qapi_event_state, evstate); 4951d95db74SKevin Wolf qobject_unref(evstate->data); 4961d95db74SKevin Wolf timer_free(evstate->timer); 4971d95db74SKevin Wolf g_free(evstate); 4981d95db74SKevin Wolf } 4991d95db74SKevin Wolf } 5001d95db74SKevin Wolf 5011d95db74SKevin Wolf static unsigned int qapi_event_throttle_hash(const void *key) 5021d95db74SKevin Wolf { 5031d95db74SKevin Wolf const MonitorQAPIEventState *evstate = key; 5041d95db74SKevin Wolf unsigned int hash = evstate->event * 255; 5051d95db74SKevin Wolf 5061d95db74SKevin Wolf if (evstate->event == QAPI_EVENT_VSERPORT_CHANGE) { 5071d95db74SKevin Wolf hash += g_str_hash(qdict_get_str(evstate->data, "id")); 5081d95db74SKevin Wolf } 5091d95db74SKevin Wolf 5101d95db74SKevin Wolf if (evstate->event == QAPI_EVENT_QUORUM_REPORT_BAD) { 5111d95db74SKevin Wolf hash += g_str_hash(qdict_get_str(evstate->data, "node-name")); 5121d95db74SKevin Wolf } 5131d95db74SKevin Wolf 51477ae2302SDavid Hildenbrand if (evstate->event == QAPI_EVENT_MEMORY_DEVICE_SIZE_CHANGE) { 51577ae2302SDavid Hildenbrand hash += g_str_hash(qdict_get_str(evstate->data, "qom-path")); 51677ae2302SDavid Hildenbrand } 51777ae2302SDavid Hildenbrand 5181d95db74SKevin Wolf return hash; 5191d95db74SKevin Wolf } 5201d95db74SKevin Wolf 5211d95db74SKevin Wolf static gboolean qapi_event_throttle_equal(const void *a, const void *b) 5221d95db74SKevin Wolf { 5231d95db74SKevin Wolf const MonitorQAPIEventState *eva = a; 5241d95db74SKevin Wolf const MonitorQAPIEventState *evb = b; 5251d95db74SKevin Wolf 5261d95db74SKevin Wolf if (eva->event != evb->event) { 5271d95db74SKevin Wolf return FALSE; 5281d95db74SKevin Wolf } 5291d95db74SKevin Wolf 5301d95db74SKevin Wolf if (eva->event == QAPI_EVENT_VSERPORT_CHANGE) { 5311d95db74SKevin Wolf return !strcmp(qdict_get_str(eva->data, "id"), 5321d95db74SKevin Wolf qdict_get_str(evb->data, "id")); 5331d95db74SKevin Wolf } 5341d95db74SKevin Wolf 5351d95db74SKevin Wolf if (eva->event == QAPI_EVENT_QUORUM_REPORT_BAD) { 5361d95db74SKevin Wolf return !strcmp(qdict_get_str(eva->data, "node-name"), 5371d95db74SKevin Wolf qdict_get_str(evb->data, "node-name")); 5381d95db74SKevin Wolf } 5391d95db74SKevin Wolf 54077ae2302SDavid Hildenbrand if (eva->event == QAPI_EVENT_MEMORY_DEVICE_SIZE_CHANGE) { 54177ae2302SDavid Hildenbrand return !strcmp(qdict_get_str(eva->data, "qom-path"), 54277ae2302SDavid Hildenbrand qdict_get_str(evb->data, "qom-path")); 54377ae2302SDavid Hildenbrand } 54477ae2302SDavid Hildenbrand 5451d95db74SKevin Wolf return TRUE; 5461d95db74SKevin Wolf } 5471d95db74SKevin Wolf 5481d95db74SKevin Wolf int monitor_suspend(Monitor *mon) 5491d95db74SKevin Wolf { 5501d95db74SKevin Wolf if (monitor_is_hmp_non_interactive(mon)) { 5511d95db74SKevin Wolf return -ENOTTY; 5521d95db74SKevin Wolf } 5531d95db74SKevin Wolf 554d73415a3SStefan Hajnoczi qatomic_inc(&mon->suspend_cnt); 5551d95db74SKevin Wolf 5561d95db74SKevin Wolf if (mon->use_io_thread) { 5571d95db74SKevin Wolf /* 5581d95db74SKevin Wolf * Kick I/O thread to make sure this takes effect. It'll be 5591d95db74SKevin Wolf * evaluated again in prepare() of the watch object. 5601d95db74SKevin Wolf */ 5611d95db74SKevin Wolf aio_notify(iothread_get_aio_context(mon_iothread)); 5621d95db74SKevin Wolf } 5631d95db74SKevin Wolf 5641d95db74SKevin Wolf trace_monitor_suspend(mon, 1); 5651d95db74SKevin Wolf return 0; 5661d95db74SKevin Wolf } 5671d95db74SKevin Wolf 5681d95db74SKevin Wolf static void monitor_accept_input(void *opaque) 5691d95db74SKevin Wolf { 5701d95db74SKevin Wolf Monitor *mon = opaque; 5711d95db74SKevin Wolf 572c5d0c55fSPaolo Bonzini if (!monitor_is_qmp(mon)) { 573c5d0c55fSPaolo Bonzini MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common); 574c5d0c55fSPaolo Bonzini assert(hmp_mon->rs); 575c5d0c55fSPaolo Bonzini readline_show_prompt(hmp_mon->rs); 576c5d0c55fSPaolo Bonzini } 577c5d0c55fSPaolo Bonzini 5781d95db74SKevin Wolf qemu_chr_fe_accept_input(&mon->chr); 5791d95db74SKevin Wolf } 5801d95db74SKevin Wolf 5811d95db74SKevin Wolf void monitor_resume(Monitor *mon) 5821d95db74SKevin Wolf { 5831d95db74SKevin Wolf if (monitor_is_hmp_non_interactive(mon)) { 5841d95db74SKevin Wolf return; 5851d95db74SKevin Wolf } 5861d95db74SKevin Wolf 587d73415a3SStefan Hajnoczi if (qatomic_dec_fetch(&mon->suspend_cnt) == 0) { 5881d95db74SKevin Wolf AioContext *ctx; 5891d95db74SKevin Wolf 5901d95db74SKevin Wolf if (mon->use_io_thread) { 5911d95db74SKevin Wolf ctx = iothread_get_aio_context(mon_iothread); 5921d95db74SKevin Wolf } else { 5931d95db74SKevin Wolf ctx = qemu_get_aio_context(); 5941d95db74SKevin Wolf } 5951d95db74SKevin Wolf 5961d95db74SKevin Wolf aio_bh_schedule_oneshot(ctx, monitor_accept_input, mon); 5971d95db74SKevin Wolf } 5981d95db74SKevin Wolf 5991d95db74SKevin Wolf trace_monitor_suspend(mon, -1); 6001d95db74SKevin Wolf } 6011d95db74SKevin Wolf 6021d95db74SKevin Wolf int monitor_can_read(void *opaque) 6031d95db74SKevin Wolf { 6041d95db74SKevin Wolf Monitor *mon = opaque; 6051d95db74SKevin Wolf 606d73415a3SStefan Hajnoczi return !qatomic_mb_read(&mon->suspend_cnt); 6071d95db74SKevin Wolf } 6081d95db74SKevin Wolf 6091d95db74SKevin Wolf void monitor_list_append(Monitor *mon) 6101d95db74SKevin Wolf { 6111d95db74SKevin Wolf qemu_mutex_lock(&monitor_lock); 6121d95db74SKevin Wolf /* 6131d95db74SKevin Wolf * This prevents inserting new monitors during monitor_cleanup(). 6141d95db74SKevin Wolf * A cleaner solution would involve the main thread telling other 6151d95db74SKevin Wolf * threads to terminate, waiting for their termination. 6161d95db74SKevin Wolf */ 6171d95db74SKevin Wolf if (!monitor_destroyed) { 6181d95db74SKevin Wolf QTAILQ_INSERT_HEAD(&mon_list, mon, entry); 6191d95db74SKevin Wolf mon = NULL; 6201d95db74SKevin Wolf } 6211d95db74SKevin Wolf qemu_mutex_unlock(&monitor_lock); 6221d95db74SKevin Wolf 6231d95db74SKevin Wolf if (mon) { 6241d95db74SKevin Wolf monitor_data_destroy(mon); 6251d95db74SKevin Wolf g_free(mon); 6261d95db74SKevin Wolf } 6271d95db74SKevin Wolf } 6281d95db74SKevin Wolf 6291d95db74SKevin Wolf static void monitor_iothread_init(void) 6301d95db74SKevin Wolf { 6311d95db74SKevin Wolf mon_iothread = iothread_create("mon_iothread", &error_abort); 6321d95db74SKevin Wolf } 6331d95db74SKevin Wolf 63492082416SKevin Wolf void monitor_data_init(Monitor *mon, bool is_qmp, bool skip_flush, 6351d95db74SKevin Wolf bool use_io_thread) 6361d95db74SKevin Wolf { 6371d95db74SKevin Wolf if (use_io_thread && !mon_iothread) { 6381d95db74SKevin Wolf monitor_iothread_init(); 6391d95db74SKevin Wolf } 6401d95db74SKevin Wolf qemu_mutex_init(&mon->mon_lock); 64192082416SKevin Wolf mon->is_qmp = is_qmp; 64220076f4aSMarkus Armbruster mon->outbuf = g_string_new(NULL); 6431d95db74SKevin Wolf mon->skip_flush = skip_flush; 6441d95db74SKevin Wolf mon->use_io_thread = use_io_thread; 6451d95db74SKevin Wolf } 6461d95db74SKevin Wolf 6471d95db74SKevin Wolf void monitor_data_destroy(Monitor *mon) 6481d95db74SKevin Wolf { 6491d95db74SKevin Wolf g_free(mon->mon_cpu_path); 6501d95db74SKevin Wolf qemu_chr_fe_deinit(&mon->chr, false); 6511d95db74SKevin Wolf if (monitor_is_qmp(mon)) { 6521d95db74SKevin Wolf monitor_data_destroy_qmp(container_of(mon, MonitorQMP, common)); 6531d95db74SKevin Wolf } else { 6541d95db74SKevin Wolf readline_free(container_of(mon, MonitorHMP, common)->rs); 6551d95db74SKevin Wolf } 65620076f4aSMarkus Armbruster g_string_free(mon->outbuf, true); 6571d95db74SKevin Wolf qemu_mutex_destroy(&mon->mon_lock); 6581d95db74SKevin Wolf } 6591d95db74SKevin Wolf 6601d95db74SKevin Wolf void monitor_cleanup(void) 6611d95db74SKevin Wolf { 6621d95db74SKevin Wolf /* 663357bda95SKevin Wolf * The dispatcher needs to stop before destroying the monitor and 664357bda95SKevin Wolf * the I/O thread. 6659ce44e2cSKevin Wolf * 6669ce44e2cSKevin Wolf * We need to poll both qemu_aio_context and iohandler_ctx to make 6679ce44e2cSKevin Wolf * sure that the dispatcher coroutine keeps making progress and 6689ce44e2cSKevin Wolf * eventually terminates. qemu_aio_context is automatically 6699612aa40SStefan Hajnoczi * polled by calling AIO_WAIT_WHILE_UNLOCKED on it, but we must poll 6709ce44e2cSKevin Wolf * iohandler_ctx manually. 671c81219a7SKevin Wolf * 672c81219a7SKevin Wolf * Letting the iothread continue while shutting down the dispatcher 673c81219a7SKevin Wolf * means that new requests may still be coming in. This is okay, 674c81219a7SKevin Wolf * we'll just leave them in the queue without sending a response 675c81219a7SKevin Wolf * and monitor_data_destroy() will free them. 6769ce44e2cSKevin Wolf */ 6779ce44e2cSKevin Wolf qmp_dispatcher_co_shutdown = true; 6789ce44e2cSKevin Wolf if (!qatomic_xchg(&qmp_dispatcher_co_busy, true)) { 6799ce44e2cSKevin Wolf aio_co_wake(qmp_dispatcher_co); 6809ce44e2cSKevin Wolf } 6819ce44e2cSKevin Wolf 6829612aa40SStefan Hajnoczi AIO_WAIT_WHILE_UNLOCKED(NULL, 6839ce44e2cSKevin Wolf (aio_poll(iohandler_get_aio_context(), false), 6849ce44e2cSKevin Wolf qatomic_mb_read(&qmp_dispatcher_co_busy))); 6859ce44e2cSKevin Wolf 686c81219a7SKevin Wolf /* 687c81219a7SKevin Wolf * We need to explicitly stop the I/O thread (but not destroy it), 688c81219a7SKevin Wolf * clean up the monitor resources, then destroy the I/O thread since 689c81219a7SKevin Wolf * we need to unregister from chardev below in 690c81219a7SKevin Wolf * monitor_data_destroy(), and chardev is not thread-safe yet 691c81219a7SKevin Wolf */ 692c81219a7SKevin Wolf if (mon_iothread) { 693c81219a7SKevin Wolf iothread_stop(mon_iothread); 694c81219a7SKevin Wolf } 695c81219a7SKevin Wolf 696357bda95SKevin Wolf /* Flush output buffers and destroy monitors */ 697357bda95SKevin Wolf qemu_mutex_lock(&monitor_lock); 698357bda95SKevin Wolf monitor_destroyed = true; 699357bda95SKevin Wolf while (!QTAILQ_EMPTY(&mon_list)) { 700357bda95SKevin Wolf Monitor *mon = QTAILQ_FIRST(&mon_list); 701357bda95SKevin Wolf QTAILQ_REMOVE(&mon_list, mon, entry); 702357bda95SKevin Wolf /* Permit QAPI event emission from character frontend release */ 703357bda95SKevin Wolf qemu_mutex_unlock(&monitor_lock); 704357bda95SKevin Wolf monitor_flush(mon); 705357bda95SKevin Wolf monitor_data_destroy(mon); 706357bda95SKevin Wolf qemu_mutex_lock(&monitor_lock); 707357bda95SKevin Wolf g_free(mon); 708357bda95SKevin Wolf } 709357bda95SKevin Wolf qemu_mutex_unlock(&monitor_lock); 710357bda95SKevin Wolf 7111d95db74SKevin Wolf if (mon_iothread) { 7121d95db74SKevin Wolf iothread_destroy(mon_iothread); 7131d95db74SKevin Wolf mon_iothread = NULL; 7141d95db74SKevin Wolf } 7151d95db74SKevin Wolf } 7161d95db74SKevin Wolf 7171d95db74SKevin Wolf static void monitor_qapi_event_init(void) 7181d95db74SKevin Wolf { 7191d95db74SKevin Wolf monitor_qapi_event_state = g_hash_table_new(qapi_event_throttle_hash, 7201d95db74SKevin Wolf qapi_event_throttle_equal); 7211d95db74SKevin Wolf } 7221d95db74SKevin Wolf 7239d2b5f2cSMarkus Armbruster void monitor_init_globals(void) 7241d95db74SKevin Wolf { 7251d95db74SKevin Wolf monitor_qapi_event_init(); 7261d95db74SKevin Wolf qemu_mutex_init(&monitor_lock); 727e69ee454SKevin Wolf coroutine_mon = g_hash_table_new(NULL, NULL); 7281d95db74SKevin Wolf 7291d95db74SKevin Wolf /* 7301d95db74SKevin Wolf * The dispatcher BH must run in the main loop thread, since we 7311d95db74SKevin Wolf * have commands assuming that context. It would be nice to get 7321d95db74SKevin Wolf * rid of those assumptions. 7331d95db74SKevin Wolf */ 7349ce44e2cSKevin Wolf qmp_dispatcher_co = qemu_coroutine_create(monitor_qmp_dispatcher_co, NULL); 7359ce44e2cSKevin Wolf qatomic_mb_set(&qmp_dispatcher_co_busy, true); 7369ce44e2cSKevin Wolf aio_co_schedule(iohandler_get_aio_context(), qmp_dispatcher_co); 7371d95db74SKevin Wolf } 7381d95db74SKevin Wolf 739a2f411c4SKevin Wolf int monitor_init(MonitorOptions *opts, bool allow_hmp, Error **errp) 740c3e95551SKevin Wolf { 74150707b39SMarkus Armbruster ERRP_GUARD(); 742c3e95551SKevin Wolf Chardev *chr; 743c3e95551SKevin Wolf 744f2098725SKevin Wolf chr = qemu_chr_find(opts->chardev); 745c3e95551SKevin Wolf if (chr == NULL) { 746f2098725SKevin Wolf error_setg(errp, "chardev \"%s\" not found", opts->chardev); 747c3e95551SKevin Wolf return -1; 748c3e95551SKevin Wolf } 749c3e95551SKevin Wolf 750a2f411c4SKevin Wolf if (!opts->has_mode) { 751a2f411c4SKevin Wolf opts->mode = allow_hmp ? MONITOR_MODE_READLINE : MONITOR_MODE_CONTROL; 752a2f411c4SKevin Wolf } 753a2f411c4SKevin Wolf 754f2098725SKevin Wolf switch (opts->mode) { 755f2098725SKevin Wolf case MONITOR_MODE_CONTROL: 75650707b39SMarkus Armbruster monitor_init_qmp(chr, opts->pretty, errp); 757f2098725SKevin Wolf break; 758f2098725SKevin Wolf case MONITOR_MODE_READLINE: 759a2f411c4SKevin Wolf if (!allow_hmp) { 760a2f411c4SKevin Wolf error_setg(errp, "Only QMP is supported"); 761a2f411c4SKevin Wolf return -1; 762a2f411c4SKevin Wolf } 763f2098725SKevin Wolf if (opts->pretty) { 764283d845cSDaniel P. Berrangé error_setg(errp, "'pretty' is not compatible with HMP monitors"); 765283d845cSDaniel P. Berrangé return -1; 766f2098725SKevin Wolf } 76750707b39SMarkus Armbruster monitor_init_hmp(chr, true, errp); 768f2098725SKevin Wolf break; 769f2098725SKevin Wolf default: 770f2098725SKevin Wolf g_assert_not_reached(); 771f2098725SKevin Wolf } 772f2098725SKevin Wolf 77350707b39SMarkus Armbruster return *errp ? -1 : 0; 774f2098725SKevin Wolf } 775f2098725SKevin Wolf 776f2098725SKevin Wolf int monitor_init_opts(QemuOpts *opts, Error **errp) 777f2098725SKevin Wolf { 778f2098725SKevin Wolf Visitor *v; 779f2098725SKevin Wolf MonitorOptions *options; 780b11a093cSMarkus Armbruster int ret; 781f2098725SKevin Wolf 782f2098725SKevin Wolf v = opts_visitor_new(opts); 783b11a093cSMarkus Armbruster visit_type_MonitorOptions(v, NULL, &options, errp); 784f2098725SKevin Wolf visit_free(v); 785b11a093cSMarkus Armbruster if (!options) { 786f2098725SKevin Wolf return -1; 787c3e95551SKevin Wolf } 788b11a093cSMarkus Armbruster 789b11a093cSMarkus Armbruster ret = monitor_init(options, true, errp); 790b11a093cSMarkus Armbruster qapi_free_MonitorOptions(options); 791b11a093cSMarkus Armbruster return ret; 792c3e95551SKevin Wolf } 793c3e95551SKevin Wolf 7941d95db74SKevin Wolf QemuOptsList qemu_mon_opts = { 7951d95db74SKevin Wolf .name = "mon", 7961d95db74SKevin Wolf .implied_opt_name = "chardev", 7971d95db74SKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head), 7981d95db74SKevin Wolf .desc = { 7991d95db74SKevin Wolf { 8001d95db74SKevin Wolf .name = "mode", 8011d95db74SKevin Wolf .type = QEMU_OPT_STRING, 8021d95db74SKevin Wolf },{ 8031d95db74SKevin Wolf .name = "chardev", 8041d95db74SKevin Wolf .type = QEMU_OPT_STRING, 8051d95db74SKevin Wolf },{ 8061d95db74SKevin Wolf .name = "pretty", 8071d95db74SKevin Wolf .type = QEMU_OPT_BOOL, 8081d95db74SKevin Wolf }, 8091d95db74SKevin Wolf { /* end of list */ } 8101d95db74SKevin Wolf }, 8111d95db74SKevin Wolf }; 812