xref: /qemu/monitor/monitor.c (revision 20076f4a)
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"
35d5938f29SMarkus Armbruster #include "sysemu/sysemu.h"
361d95db74SKevin Wolf #include "trace.h"
371d95db74SKevin Wolf 
381d95db74SKevin Wolf /*
391d95db74SKevin Wolf  * To prevent flooding clients, events can be throttled. The
401d95db74SKevin Wolf  * throttling is calculated globally, rather than per-Monitor
411d95db74SKevin Wolf  * instance.
421d95db74SKevin Wolf  */
431d95db74SKevin Wolf typedef struct MonitorQAPIEventState {
441d95db74SKevin Wolf     QAPIEvent event;    /* Throttling state for this event type and... */
451d95db74SKevin Wolf     QDict *data;        /* ... data, see qapi_event_throttle_equal() */
461d95db74SKevin Wolf     QEMUTimer *timer;   /* Timer for handling delayed events */
471d95db74SKevin Wolf     QDict *qdict;       /* Delayed event (if any) */
481d95db74SKevin Wolf } MonitorQAPIEventState;
491d95db74SKevin Wolf 
501d95db74SKevin Wolf typedef struct {
511d95db74SKevin Wolf     int64_t rate;       /* Minimum time (in ns) between two events */
521d95db74SKevin Wolf } MonitorQAPIEventConf;
531d95db74SKevin Wolf 
541d95db74SKevin Wolf /* Shared monitor I/O thread */
551d95db74SKevin Wolf IOThread *mon_iothread;
561d95db74SKevin Wolf 
579ce44e2cSKevin Wolf /* Coroutine to dispatch the requests received from I/O thread */
589ce44e2cSKevin Wolf Coroutine *qmp_dispatcher_co;
599ce44e2cSKevin Wolf 
609ce44e2cSKevin Wolf /* Set to true when the dispatcher coroutine should terminate */
619ce44e2cSKevin Wolf bool qmp_dispatcher_co_shutdown;
629ce44e2cSKevin Wolf 
639ce44e2cSKevin Wolf /*
649ce44e2cSKevin Wolf  * qmp_dispatcher_co_busy is used for synchronisation between the
659ce44e2cSKevin Wolf  * monitor thread and the main thread to ensure that the dispatcher
669ce44e2cSKevin Wolf  * coroutine never gets scheduled a second time when it's already
679ce44e2cSKevin Wolf  * scheduled (scheduling the same coroutine twice is forbidden).
689ce44e2cSKevin Wolf  *
699ce44e2cSKevin Wolf  * It is true if the coroutine is active and processing requests.
709ce44e2cSKevin Wolf  * Additional requests may then be pushed onto mon->qmp_requests,
719ce44e2cSKevin Wolf  * and @qmp_dispatcher_co_shutdown may be set without further ado.
729ce44e2cSKevin Wolf  * @qmp_dispatcher_co_busy must not be woken up in this case.
739ce44e2cSKevin Wolf  *
749ce44e2cSKevin Wolf  * If false, you also have to set @qmp_dispatcher_co_busy to true and
759ce44e2cSKevin Wolf  * wake up @qmp_dispatcher_co after pushing the new requests.
769ce44e2cSKevin Wolf  *
779ce44e2cSKevin Wolf  * The coroutine will automatically change this variable back to false
789ce44e2cSKevin Wolf  * before it yields.  Nobody else may set the variable to false.
799ce44e2cSKevin Wolf  *
809ce44e2cSKevin Wolf  * Access must be atomic for thread safety.
819ce44e2cSKevin Wolf  */
829ce44e2cSKevin Wolf bool qmp_dispatcher_co_busy;
831d95db74SKevin Wolf 
84e69ee454SKevin Wolf /*
85e69ee454SKevin Wolf  * Protects mon_list, monitor_qapi_event_state, coroutine_mon,
86e69ee454SKevin Wolf  * monitor_destroyed.
87e69ee454SKevin Wolf  */
881d95db74SKevin Wolf QemuMutex monitor_lock;
891d95db74SKevin Wolf static GHashTable *monitor_qapi_event_state;
90e69ee454SKevin Wolf static GHashTable *coroutine_mon; /* Maps Coroutine* to Monitor* */
911d95db74SKevin Wolf 
921d95db74SKevin Wolf MonitorList mon_list;
931d95db74SKevin Wolf int mon_refcount;
941d95db74SKevin Wolf static bool monitor_destroyed;
951d95db74SKevin Wolf 
96947e4744SKevin Wolf Monitor *monitor_cur(void)
97947e4744SKevin Wolf {
98e69ee454SKevin Wolf     Monitor *mon;
99e69ee454SKevin Wolf 
100e69ee454SKevin Wolf     qemu_mutex_lock(&monitor_lock);
101e69ee454SKevin Wolf     mon = g_hash_table_lookup(coroutine_mon, qemu_coroutine_self());
102e69ee454SKevin Wolf     qemu_mutex_unlock(&monitor_lock);
103e69ee454SKevin Wolf 
104e69ee454SKevin Wolf     return mon;
105947e4744SKevin Wolf }
106947e4744SKevin Wolf 
107947e4744SKevin Wolf /**
108947e4744SKevin Wolf  * Sets a new current monitor and returns the old one.
109e69ee454SKevin Wolf  *
110e69ee454SKevin Wolf  * If a non-NULL monitor is set for a coroutine, another call
111e69ee454SKevin Wolf  * resetting it to NULL is required before the coroutine terminates,
112e69ee454SKevin Wolf  * otherwise a stale entry would remain in the hash table.
113947e4744SKevin Wolf  */
114e69ee454SKevin Wolf Monitor *monitor_set_cur(Coroutine *co, Monitor *mon)
115947e4744SKevin Wolf {
116e69ee454SKevin Wolf     Monitor *old_monitor = monitor_cur();
117947e4744SKevin Wolf 
118e69ee454SKevin Wolf     qemu_mutex_lock(&monitor_lock);
119e69ee454SKevin Wolf     if (mon) {
120e69ee454SKevin Wolf         g_hash_table_replace(coroutine_mon, co, mon);
121e69ee454SKevin Wolf     } else {
122e69ee454SKevin Wolf         g_hash_table_remove(coroutine_mon, co);
123e69ee454SKevin Wolf     }
124e69ee454SKevin Wolf     qemu_mutex_unlock(&monitor_lock);
125e69ee454SKevin Wolf 
126947e4744SKevin Wolf     return old_monitor;
127947e4744SKevin Wolf }
1281d95db74SKevin Wolf 
1291d95db74SKevin Wolf /**
1301d95db74SKevin Wolf  * Is the current monitor, if any, a QMP monitor?
1311d95db74SKevin Wolf  */
1321d95db74SKevin Wolf bool monitor_cur_is_qmp(void)
1331d95db74SKevin Wolf {
134947e4744SKevin Wolf     Monitor *cur_mon = monitor_cur();
135947e4744SKevin Wolf 
1361d95db74SKevin Wolf     return cur_mon && monitor_is_qmp(cur_mon);
1371d95db74SKevin Wolf }
1381d95db74SKevin Wolf 
1391d95db74SKevin Wolf /**
1401d95db74SKevin Wolf  * Is @mon is using readline?
1411d95db74SKevin Wolf  * Note: not all HMP monitors use readline, e.g., gdbserver has a
1421d95db74SKevin Wolf  * non-interactive HMP monitor, so readline is not used there.
1431d95db74SKevin Wolf  */
14492082416SKevin Wolf static inline bool monitor_uses_readline(const MonitorHMP *mon)
1451d95db74SKevin Wolf {
14692082416SKevin Wolf     return mon->use_readline;
1471d95db74SKevin Wolf }
1481d95db74SKevin Wolf 
1491d95db74SKevin Wolf static inline bool monitor_is_hmp_non_interactive(const Monitor *mon)
1501d95db74SKevin Wolf {
15192082416SKevin Wolf     if (monitor_is_qmp(mon)) {
15292082416SKevin Wolf         return false;
15392082416SKevin Wolf     }
15492082416SKevin Wolf 
15592082416SKevin Wolf     return !monitor_uses_readline(container_of(mon, MonitorHMP, common));
1561d95db74SKevin Wolf }
1571d95db74SKevin Wolf 
1581d95db74SKevin Wolf static void monitor_flush_locked(Monitor *mon);
1591d95db74SKevin Wolf 
1601d95db74SKevin Wolf static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
1611d95db74SKevin Wolf                                   void *opaque)
1621d95db74SKevin Wolf {
1631d95db74SKevin Wolf     Monitor *mon = opaque;
1641d95db74SKevin Wolf 
1651d95db74SKevin Wolf     qemu_mutex_lock(&mon->mon_lock);
1661d95db74SKevin Wolf     mon->out_watch = 0;
1671d95db74SKevin Wolf     monitor_flush_locked(mon);
1681d95db74SKevin Wolf     qemu_mutex_unlock(&mon->mon_lock);
1691d95db74SKevin Wolf     return FALSE;
1701d95db74SKevin Wolf }
1711d95db74SKevin Wolf 
1721d95db74SKevin Wolf /* Caller must hold mon->mon_lock */
1731d95db74SKevin Wolf static void monitor_flush_locked(Monitor *mon)
1741d95db74SKevin Wolf {
1751d95db74SKevin Wolf     int rc;
1761d95db74SKevin Wolf     size_t len;
1771d95db74SKevin Wolf     const char *buf;
1781d95db74SKevin Wolf 
1791d95db74SKevin Wolf     if (mon->skip_flush) {
1801d95db74SKevin Wolf         return;
1811d95db74SKevin Wolf     }
1821d95db74SKevin Wolf 
183*20076f4aSMarkus Armbruster     buf = mon->outbuf->str;
184*20076f4aSMarkus Armbruster     len = mon->outbuf->len;
1851d95db74SKevin Wolf 
1861d95db74SKevin Wolf     if (len && !mon->mux_out) {
1871d95db74SKevin Wolf         rc = qemu_chr_fe_write(&mon->chr, (const uint8_t *) buf, len);
1881d95db74SKevin Wolf         if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
1891d95db74SKevin Wolf             /* all flushed or error */
190*20076f4aSMarkus Armbruster             g_string_truncate(mon->outbuf, 0);
1911d95db74SKevin Wolf             return;
1921d95db74SKevin Wolf         }
1931d95db74SKevin Wolf         if (rc > 0) {
1941d95db74SKevin Wolf             /* partial write */
195*20076f4aSMarkus Armbruster             g_string_erase(mon->outbuf, 0, rc);
1961d95db74SKevin Wolf         }
1971d95db74SKevin Wolf         if (mon->out_watch == 0) {
1981d95db74SKevin Wolf             mon->out_watch =
1991d95db74SKevin Wolf                 qemu_chr_fe_add_watch(&mon->chr, G_IO_OUT | G_IO_HUP,
2001d95db74SKevin Wolf                                       monitor_unblocked, mon);
2011d95db74SKevin Wolf         }
2021d95db74SKevin Wolf     }
2031d95db74SKevin Wolf }
2041d95db74SKevin Wolf 
2051d95db74SKevin Wolf void monitor_flush(Monitor *mon)
2061d95db74SKevin Wolf {
2071d95db74SKevin Wolf     qemu_mutex_lock(&mon->mon_lock);
2081d95db74SKevin Wolf     monitor_flush_locked(mon);
2091d95db74SKevin Wolf     qemu_mutex_unlock(&mon->mon_lock);
2101d95db74SKevin Wolf }
2111d95db74SKevin Wolf 
2121d95db74SKevin Wolf /* flush at every end of line */
2131d95db74SKevin Wolf int monitor_puts(Monitor *mon, const char *str)
2141d95db74SKevin Wolf {
2151d95db74SKevin Wolf     int i;
2161d95db74SKevin Wolf     char c;
2171d95db74SKevin Wolf 
2181d95db74SKevin Wolf     qemu_mutex_lock(&mon->mon_lock);
2191d95db74SKevin Wolf     for (i = 0; str[i]; i++) {
2201d95db74SKevin Wolf         c = str[i];
2211d95db74SKevin Wolf         if (c == '\n') {
222*20076f4aSMarkus Armbruster             g_string_append_c(mon->outbuf, '\r');
2231d95db74SKevin Wolf         }
224*20076f4aSMarkus Armbruster         g_string_append_c(mon->outbuf, c);
2251d95db74SKevin Wolf         if (c == '\n') {
2261d95db74SKevin Wolf             monitor_flush_locked(mon);
2271d95db74SKevin Wolf         }
2281d95db74SKevin Wolf     }
2291d95db74SKevin Wolf     qemu_mutex_unlock(&mon->mon_lock);
2301d95db74SKevin Wolf 
2311d95db74SKevin Wolf     return i;
2321d95db74SKevin Wolf }
2331d95db74SKevin Wolf 
2341d95db74SKevin Wolf int monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
2351d95db74SKevin Wolf {
2361d95db74SKevin Wolf     char *buf;
2371d95db74SKevin Wolf     int n;
2381d95db74SKevin Wolf 
2391d95db74SKevin Wolf     if (!mon) {
2401d95db74SKevin Wolf         return -1;
2411d95db74SKevin Wolf     }
2421d95db74SKevin Wolf 
2431d95db74SKevin Wolf     if (monitor_is_qmp(mon)) {
2441d95db74SKevin Wolf         return -1;
2451d95db74SKevin Wolf     }
2461d95db74SKevin Wolf 
2471d95db74SKevin Wolf     buf = g_strdup_vprintf(fmt, ap);
2481d95db74SKevin Wolf     n = monitor_puts(mon, buf);
2491d95db74SKevin Wolf     g_free(buf);
2501d95db74SKevin Wolf     return n;
2511d95db74SKevin Wolf }
2521d95db74SKevin Wolf 
2531d95db74SKevin Wolf int monitor_printf(Monitor *mon, const char *fmt, ...)
2541d95db74SKevin Wolf {
2551d95db74SKevin Wolf     int ret;
2561d95db74SKevin Wolf 
2571d95db74SKevin Wolf     va_list ap;
2581d95db74SKevin Wolf     va_start(ap, fmt);
2591d95db74SKevin Wolf     ret = monitor_vprintf(mon, fmt, ap);
2601d95db74SKevin Wolf     va_end(ap);
2611d95db74SKevin Wolf     return ret;
2621d95db74SKevin Wolf }
2631d95db74SKevin Wolf 
2641d95db74SKevin Wolf /*
2651d95db74SKevin Wolf  * Print to current monitor if we have one, else to stderr.
2661d95db74SKevin Wolf  */
2671d95db74SKevin Wolf int error_vprintf(const char *fmt, va_list ap)
2681d95db74SKevin Wolf {
269947e4744SKevin Wolf     Monitor *cur_mon = monitor_cur();
270947e4744SKevin Wolf 
2711d95db74SKevin Wolf     if (cur_mon && !monitor_cur_is_qmp()) {
2721d95db74SKevin Wolf         return monitor_vprintf(cur_mon, fmt, ap);
2731d95db74SKevin Wolf     }
2741d95db74SKevin Wolf     return vfprintf(stderr, fmt, ap);
2751d95db74SKevin Wolf }
2761d95db74SKevin Wolf 
2771d95db74SKevin Wolf int error_vprintf_unless_qmp(const char *fmt, va_list ap)
2781d95db74SKevin Wolf {
279947e4744SKevin Wolf     Monitor *cur_mon = monitor_cur();
280947e4744SKevin Wolf 
2811d95db74SKevin Wolf     if (!cur_mon) {
2821d95db74SKevin Wolf         return vfprintf(stderr, fmt, ap);
2831d95db74SKevin Wolf     }
2841d95db74SKevin Wolf     if (!monitor_cur_is_qmp()) {
2851d95db74SKevin Wolf         return monitor_vprintf(cur_mon, fmt, ap);
2861d95db74SKevin Wolf     }
2871d95db74SKevin Wolf     return -1;
2881d95db74SKevin Wolf }
2891d95db74SKevin Wolf 
2901d95db74SKevin Wolf 
2911d95db74SKevin Wolf static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = {
2921d95db74SKevin Wolf     /* Limit guest-triggerable events to 1 per second */
2931d95db74SKevin Wolf     [QAPI_EVENT_RTC_CHANGE]        = { 1000 * SCALE_MS },
2941d95db74SKevin Wolf     [QAPI_EVENT_WATCHDOG]          = { 1000 * SCALE_MS },
2951d95db74SKevin Wolf     [QAPI_EVENT_BALLOON_CHANGE]    = { 1000 * SCALE_MS },
2961d95db74SKevin Wolf     [QAPI_EVENT_QUORUM_REPORT_BAD] = { 1000 * SCALE_MS },
2971d95db74SKevin Wolf     [QAPI_EVENT_QUORUM_FAILURE]    = { 1000 * SCALE_MS },
2981d95db74SKevin Wolf     [QAPI_EVENT_VSERPORT_CHANGE]   = { 1000 * SCALE_MS },
299722a3c78SDavid Hildenbrand     [QAPI_EVENT_MEMORY_DEVICE_SIZE_CHANGE] = { 1000 * SCALE_MS },
3001d95db74SKevin Wolf };
3011d95db74SKevin Wolf 
3021d95db74SKevin Wolf /*
3031d95db74SKevin Wolf  * Return the clock to use for recording an event's time.
3041d95db74SKevin Wolf  * It's QEMU_CLOCK_REALTIME, except for qtests it's
3051d95db74SKevin Wolf  * QEMU_CLOCK_VIRTUAL, to support testing rate limits.
3061d95db74SKevin Wolf  * Beware: result is invalid before configure_accelerator().
3071d95db74SKevin Wolf  */
3081d95db74SKevin Wolf static inline QEMUClockType monitor_get_event_clock(void)
3091d95db74SKevin Wolf {
3101d95db74SKevin Wolf     return qtest_enabled() ? QEMU_CLOCK_VIRTUAL : QEMU_CLOCK_REALTIME;
3111d95db74SKevin Wolf }
3121d95db74SKevin Wolf 
3131d95db74SKevin Wolf /*
3141d95db74SKevin Wolf  * Broadcast an event to all monitors.
3151d95db74SKevin Wolf  * @qdict is the event object.  Its member "event" must match @event.
3161d95db74SKevin Wolf  * Caller must hold monitor_lock.
3171d95db74SKevin Wolf  */
3181d95db74SKevin Wolf static void monitor_qapi_event_emit(QAPIEvent event, QDict *qdict)
3191d95db74SKevin Wolf {
3201d95db74SKevin Wolf     Monitor *mon;
3211d95db74SKevin Wolf     MonitorQMP *qmp_mon;
3221d95db74SKevin Wolf 
3231d95db74SKevin Wolf     trace_monitor_protocol_event_emit(event, qdict);
3241d95db74SKevin Wolf     QTAILQ_FOREACH(mon, &mon_list, entry) {
3251d95db74SKevin Wolf         if (!monitor_is_qmp(mon)) {
3261d95db74SKevin Wolf             continue;
3271d95db74SKevin Wolf         }
3281d95db74SKevin Wolf 
3291d95db74SKevin Wolf         qmp_mon = container_of(mon, MonitorQMP, common);
3301d95db74SKevin Wolf         if (qmp_mon->commands != &qmp_cap_negotiation_commands) {
3311d95db74SKevin Wolf             qmp_send_response(qmp_mon, qdict);
3321d95db74SKevin Wolf         }
3331d95db74SKevin Wolf     }
3341d95db74SKevin Wolf }
3351d95db74SKevin Wolf 
3361d95db74SKevin Wolf static void monitor_qapi_event_handler(void *opaque);
3371d95db74SKevin Wolf 
3381d95db74SKevin Wolf /*
3391d95db74SKevin Wolf  * Queue a new event for emission to Monitor instances,
3401d95db74SKevin Wolf  * applying any rate limiting if required.
3411d95db74SKevin Wolf  */
3421d95db74SKevin Wolf static void
3431d95db74SKevin Wolf monitor_qapi_event_queue_no_reenter(QAPIEvent event, QDict *qdict)
3441d95db74SKevin Wolf {
3451d95db74SKevin Wolf     MonitorQAPIEventConf *evconf;
3461d95db74SKevin Wolf     MonitorQAPIEventState *evstate;
3471d95db74SKevin Wolf 
3481d95db74SKevin Wolf     assert(event < QAPI_EVENT__MAX);
3491d95db74SKevin Wolf     evconf = &monitor_qapi_event_conf[event];
3501d95db74SKevin Wolf     trace_monitor_protocol_event_queue(event, qdict, evconf->rate);
3511d95db74SKevin Wolf 
3521d95db74SKevin Wolf     qemu_mutex_lock(&monitor_lock);
3531d95db74SKevin Wolf 
3541d95db74SKevin Wolf     if (!evconf->rate) {
3551d95db74SKevin Wolf         /* Unthrottled event */
3561d95db74SKevin Wolf         monitor_qapi_event_emit(event, qdict);
3571d95db74SKevin Wolf     } else {
3581d95db74SKevin Wolf         QDict *data = qobject_to(QDict, qdict_get(qdict, "data"));
3591d95db74SKevin Wolf         MonitorQAPIEventState key = { .event = event, .data = data };
3601d95db74SKevin Wolf 
3611d95db74SKevin Wolf         evstate = g_hash_table_lookup(monitor_qapi_event_state, &key);
3621d95db74SKevin Wolf         assert(!evstate || timer_pending(evstate->timer));
3631d95db74SKevin Wolf 
3641d95db74SKevin Wolf         if (evstate) {
3651d95db74SKevin Wolf             /*
3661d95db74SKevin Wolf              * Timer is pending for (at least) evconf->rate ns after
3671d95db74SKevin Wolf              * last send.  Store event for sending when timer fires,
3681d95db74SKevin Wolf              * replacing a prior stored event if any.
3691d95db74SKevin Wolf              */
3701d95db74SKevin Wolf             qobject_unref(evstate->qdict);
3711d95db74SKevin Wolf             evstate->qdict = qobject_ref(qdict);
3721d95db74SKevin Wolf         } else {
3731d95db74SKevin Wolf             /*
3741d95db74SKevin Wolf              * Last send was (at least) evconf->rate ns ago.
3751d95db74SKevin Wolf              * Send immediately, and arm the timer to call
3761d95db74SKevin Wolf              * monitor_qapi_event_handler() in evconf->rate ns.  Any
3771d95db74SKevin Wolf              * events arriving before then will be delayed until then.
3781d95db74SKevin Wolf              */
3791d95db74SKevin Wolf             int64_t now = qemu_clock_get_ns(monitor_get_event_clock());
3801d95db74SKevin Wolf 
3811d95db74SKevin Wolf             monitor_qapi_event_emit(event, qdict);
3821d95db74SKevin Wolf 
3831d95db74SKevin Wolf             evstate = g_new(MonitorQAPIEventState, 1);
3841d95db74SKevin Wolf             evstate->event = event;
3851d95db74SKevin Wolf             evstate->data = qobject_ref(data);
3861d95db74SKevin Wolf             evstate->qdict = NULL;
3871d95db74SKevin Wolf             evstate->timer = timer_new_ns(monitor_get_event_clock(),
3881d95db74SKevin Wolf                                           monitor_qapi_event_handler,
3891d95db74SKevin Wolf                                           evstate);
3901d95db74SKevin Wolf             g_hash_table_add(monitor_qapi_event_state, evstate);
3911d95db74SKevin Wolf             timer_mod_ns(evstate->timer, now + evconf->rate);
3921d95db74SKevin Wolf         }
3931d95db74SKevin Wolf     }
3941d95db74SKevin Wolf 
3951d95db74SKevin Wolf     qemu_mutex_unlock(&monitor_lock);
3961d95db74SKevin Wolf }
3971d95db74SKevin Wolf 
3981d95db74SKevin Wolf void qapi_event_emit(QAPIEvent event, QDict *qdict)
3991d95db74SKevin Wolf {
4001d95db74SKevin Wolf     /*
4011d95db74SKevin Wolf      * monitor_qapi_event_queue_no_reenter() is not reentrant: it
4021d95db74SKevin Wolf      * would deadlock on monitor_lock.  Work around by queueing
4031d95db74SKevin Wolf      * events in thread-local storage.
4041d95db74SKevin Wolf      * TODO: remove this, make it re-enter safe.
4051d95db74SKevin Wolf      */
4061d95db74SKevin Wolf     typedef struct MonitorQapiEvent {
4071d95db74SKevin Wolf         QAPIEvent event;
4081d95db74SKevin Wolf         QDict *qdict;
4091d95db74SKevin Wolf         QSIMPLEQ_ENTRY(MonitorQapiEvent) entry;
4101d95db74SKevin Wolf     } MonitorQapiEvent;
4111d95db74SKevin Wolf     static __thread QSIMPLEQ_HEAD(, MonitorQapiEvent) event_queue;
4121d95db74SKevin Wolf     static __thread bool reentered;
4131d95db74SKevin Wolf     MonitorQapiEvent *ev;
4141d95db74SKevin Wolf 
4151d95db74SKevin Wolf     if (!reentered) {
4161d95db74SKevin Wolf         QSIMPLEQ_INIT(&event_queue);
4171d95db74SKevin Wolf     }
4181d95db74SKevin Wolf 
4191d95db74SKevin Wolf     ev = g_new(MonitorQapiEvent, 1);
4201d95db74SKevin Wolf     ev->qdict = qobject_ref(qdict);
4211d95db74SKevin Wolf     ev->event = event;
4221d95db74SKevin Wolf     QSIMPLEQ_INSERT_TAIL(&event_queue, ev, entry);
4231d95db74SKevin Wolf     if (reentered) {
4241d95db74SKevin Wolf         return;
4251d95db74SKevin Wolf     }
4261d95db74SKevin Wolf 
4271d95db74SKevin Wolf     reentered = true;
4281d95db74SKevin Wolf 
4291d95db74SKevin Wolf     while ((ev = QSIMPLEQ_FIRST(&event_queue)) != NULL) {
4301d95db74SKevin Wolf         QSIMPLEQ_REMOVE_HEAD(&event_queue, entry);
4311d95db74SKevin Wolf         monitor_qapi_event_queue_no_reenter(ev->event, ev->qdict);
4321d95db74SKevin Wolf         qobject_unref(ev->qdict);
4331d95db74SKevin Wolf         g_free(ev);
4341d95db74SKevin Wolf     }
4351d95db74SKevin Wolf 
4361d95db74SKevin Wolf     reentered = false;
4371d95db74SKevin Wolf }
4381d95db74SKevin Wolf 
4391d95db74SKevin Wolf /*
4401d95db74SKevin Wolf  * This function runs evconf->rate ns after sending a throttled
4411d95db74SKevin Wolf  * event.
4421d95db74SKevin Wolf  * If another event has since been stored, send it.
4431d95db74SKevin Wolf  */
4441d95db74SKevin Wolf static void monitor_qapi_event_handler(void *opaque)
4451d95db74SKevin Wolf {
4461d95db74SKevin Wolf     MonitorQAPIEventState *evstate = opaque;
4471d95db74SKevin Wolf     MonitorQAPIEventConf *evconf = &monitor_qapi_event_conf[evstate->event];
4481d95db74SKevin Wolf 
4491d95db74SKevin Wolf     trace_monitor_protocol_event_handler(evstate->event, evstate->qdict);
4501d95db74SKevin Wolf     qemu_mutex_lock(&monitor_lock);
4511d95db74SKevin Wolf 
4521d95db74SKevin Wolf     if (evstate->qdict) {
4531d95db74SKevin Wolf         int64_t now = qemu_clock_get_ns(monitor_get_event_clock());
4541d95db74SKevin Wolf 
4551d95db74SKevin Wolf         monitor_qapi_event_emit(evstate->event, evstate->qdict);
4561d95db74SKevin Wolf         qobject_unref(evstate->qdict);
4571d95db74SKevin Wolf         evstate->qdict = NULL;
4581d95db74SKevin Wolf         timer_mod_ns(evstate->timer, now + evconf->rate);
4591d95db74SKevin Wolf     } else {
4601d95db74SKevin Wolf         g_hash_table_remove(monitor_qapi_event_state, evstate);
4611d95db74SKevin Wolf         qobject_unref(evstate->data);
4621d95db74SKevin Wolf         timer_free(evstate->timer);
4631d95db74SKevin Wolf         g_free(evstate);
4641d95db74SKevin Wolf     }
4651d95db74SKevin Wolf 
4661d95db74SKevin Wolf     qemu_mutex_unlock(&monitor_lock);
4671d95db74SKevin Wolf }
4681d95db74SKevin Wolf 
4691d95db74SKevin Wolf static unsigned int qapi_event_throttle_hash(const void *key)
4701d95db74SKevin Wolf {
4711d95db74SKevin Wolf     const MonitorQAPIEventState *evstate = key;
4721d95db74SKevin Wolf     unsigned int hash = evstate->event * 255;
4731d95db74SKevin Wolf 
4741d95db74SKevin Wolf     if (evstate->event == QAPI_EVENT_VSERPORT_CHANGE) {
4751d95db74SKevin Wolf         hash += g_str_hash(qdict_get_str(evstate->data, "id"));
4761d95db74SKevin Wolf     }
4771d95db74SKevin Wolf 
4781d95db74SKevin Wolf     if (evstate->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
4791d95db74SKevin Wolf         hash += g_str_hash(qdict_get_str(evstate->data, "node-name"));
4801d95db74SKevin Wolf     }
4811d95db74SKevin Wolf 
4821d95db74SKevin Wolf     return hash;
4831d95db74SKevin Wolf }
4841d95db74SKevin Wolf 
4851d95db74SKevin Wolf static gboolean qapi_event_throttle_equal(const void *a, const void *b)
4861d95db74SKevin Wolf {
4871d95db74SKevin Wolf     const MonitorQAPIEventState *eva = a;
4881d95db74SKevin Wolf     const MonitorQAPIEventState *evb = b;
4891d95db74SKevin Wolf 
4901d95db74SKevin Wolf     if (eva->event != evb->event) {
4911d95db74SKevin Wolf         return FALSE;
4921d95db74SKevin Wolf     }
4931d95db74SKevin Wolf 
4941d95db74SKevin Wolf     if (eva->event == QAPI_EVENT_VSERPORT_CHANGE) {
4951d95db74SKevin Wolf         return !strcmp(qdict_get_str(eva->data, "id"),
4961d95db74SKevin Wolf                        qdict_get_str(evb->data, "id"));
4971d95db74SKevin Wolf     }
4981d95db74SKevin Wolf 
4991d95db74SKevin Wolf     if (eva->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
5001d95db74SKevin Wolf         return !strcmp(qdict_get_str(eva->data, "node-name"),
5011d95db74SKevin Wolf                        qdict_get_str(evb->data, "node-name"));
5021d95db74SKevin Wolf     }
5031d95db74SKevin Wolf 
5041d95db74SKevin Wolf     return TRUE;
5051d95db74SKevin Wolf }
5061d95db74SKevin Wolf 
5071d95db74SKevin Wolf int monitor_suspend(Monitor *mon)
5081d95db74SKevin Wolf {
5091d95db74SKevin Wolf     if (monitor_is_hmp_non_interactive(mon)) {
5101d95db74SKevin Wolf         return -ENOTTY;
5111d95db74SKevin Wolf     }
5121d95db74SKevin Wolf 
513d73415a3SStefan Hajnoczi     qatomic_inc(&mon->suspend_cnt);
5141d95db74SKevin Wolf 
5151d95db74SKevin Wolf     if (mon->use_io_thread) {
5161d95db74SKevin Wolf         /*
5171d95db74SKevin Wolf          * Kick I/O thread to make sure this takes effect.  It'll be
5181d95db74SKevin Wolf          * evaluated again in prepare() of the watch object.
5191d95db74SKevin Wolf          */
5201d95db74SKevin Wolf         aio_notify(iothread_get_aio_context(mon_iothread));
5211d95db74SKevin Wolf     }
5221d95db74SKevin Wolf 
5231d95db74SKevin Wolf     trace_monitor_suspend(mon, 1);
5241d95db74SKevin Wolf     return 0;
5251d95db74SKevin Wolf }
5261d95db74SKevin Wolf 
5271d95db74SKevin Wolf static void monitor_accept_input(void *opaque)
5281d95db74SKevin Wolf {
5291d95db74SKevin Wolf     Monitor *mon = opaque;
5301d95db74SKevin Wolf 
5311d95db74SKevin Wolf     qemu_chr_fe_accept_input(&mon->chr);
5321d95db74SKevin Wolf }
5331d95db74SKevin Wolf 
5341d95db74SKevin Wolf void monitor_resume(Monitor *mon)
5351d95db74SKevin Wolf {
5361d95db74SKevin Wolf     if (monitor_is_hmp_non_interactive(mon)) {
5371d95db74SKevin Wolf         return;
5381d95db74SKevin Wolf     }
5391d95db74SKevin Wolf 
540d73415a3SStefan Hajnoczi     if (qatomic_dec_fetch(&mon->suspend_cnt) == 0) {
5411d95db74SKevin Wolf         AioContext *ctx;
5421d95db74SKevin Wolf 
5431d95db74SKevin Wolf         if (mon->use_io_thread) {
5441d95db74SKevin Wolf             ctx = iothread_get_aio_context(mon_iothread);
5451d95db74SKevin Wolf         } else {
5461d95db74SKevin Wolf             ctx = qemu_get_aio_context();
5471d95db74SKevin Wolf         }
5481d95db74SKevin Wolf 
5491d95db74SKevin Wolf         if (!monitor_is_qmp(mon)) {
5501d95db74SKevin Wolf             MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common);
5511d95db74SKevin Wolf             assert(hmp_mon->rs);
5521d95db74SKevin Wolf             readline_show_prompt(hmp_mon->rs);
5531d95db74SKevin Wolf         }
5541d95db74SKevin Wolf 
5551d95db74SKevin Wolf         aio_bh_schedule_oneshot(ctx, monitor_accept_input, mon);
5561d95db74SKevin Wolf     }
5571d95db74SKevin Wolf 
5581d95db74SKevin Wolf     trace_monitor_suspend(mon, -1);
5591d95db74SKevin Wolf }
5601d95db74SKevin Wolf 
5611d95db74SKevin Wolf int monitor_can_read(void *opaque)
5621d95db74SKevin Wolf {
5631d95db74SKevin Wolf     Monitor *mon = opaque;
5641d95db74SKevin Wolf 
565d73415a3SStefan Hajnoczi     return !qatomic_mb_read(&mon->suspend_cnt);
5661d95db74SKevin Wolf }
5671d95db74SKevin Wolf 
5681d95db74SKevin Wolf void monitor_list_append(Monitor *mon)
5691d95db74SKevin Wolf {
5701d95db74SKevin Wolf     qemu_mutex_lock(&monitor_lock);
5711d95db74SKevin Wolf     /*
5721d95db74SKevin Wolf      * This prevents inserting new monitors during monitor_cleanup().
5731d95db74SKevin Wolf      * A cleaner solution would involve the main thread telling other
5741d95db74SKevin Wolf      * threads to terminate, waiting for their termination.
5751d95db74SKevin Wolf      */
5761d95db74SKevin Wolf     if (!monitor_destroyed) {
5771d95db74SKevin Wolf         QTAILQ_INSERT_HEAD(&mon_list, mon, entry);
5781d95db74SKevin Wolf         mon = NULL;
5791d95db74SKevin Wolf     }
5801d95db74SKevin Wolf     qemu_mutex_unlock(&monitor_lock);
5811d95db74SKevin Wolf 
5821d95db74SKevin Wolf     if (mon) {
5831d95db74SKevin Wolf         monitor_data_destroy(mon);
5841d95db74SKevin Wolf         g_free(mon);
5851d95db74SKevin Wolf     }
5861d95db74SKevin Wolf }
5871d95db74SKevin Wolf 
5881d95db74SKevin Wolf static void monitor_iothread_init(void)
5891d95db74SKevin Wolf {
5901d95db74SKevin Wolf     mon_iothread = iothread_create("mon_iothread", &error_abort);
5911d95db74SKevin Wolf }
5921d95db74SKevin Wolf 
59392082416SKevin Wolf void monitor_data_init(Monitor *mon, bool is_qmp, bool skip_flush,
5941d95db74SKevin Wolf                        bool use_io_thread)
5951d95db74SKevin Wolf {
5961d95db74SKevin Wolf     if (use_io_thread && !mon_iothread) {
5971d95db74SKevin Wolf         monitor_iothread_init();
5981d95db74SKevin Wolf     }
5991d95db74SKevin Wolf     qemu_mutex_init(&mon->mon_lock);
60092082416SKevin Wolf     mon->is_qmp = is_qmp;
601*20076f4aSMarkus Armbruster     mon->outbuf = g_string_new(NULL);
6021d95db74SKevin Wolf     mon->skip_flush = skip_flush;
6031d95db74SKevin Wolf     mon->use_io_thread = use_io_thread;
6041d95db74SKevin Wolf }
6051d95db74SKevin Wolf 
6061d95db74SKevin Wolf void monitor_data_destroy(Monitor *mon)
6071d95db74SKevin Wolf {
6081d95db74SKevin Wolf     g_free(mon->mon_cpu_path);
6091d95db74SKevin Wolf     qemu_chr_fe_deinit(&mon->chr, false);
6101d95db74SKevin Wolf     if (monitor_is_qmp(mon)) {
6111d95db74SKevin Wolf         monitor_data_destroy_qmp(container_of(mon, MonitorQMP, common));
6121d95db74SKevin Wolf     } else {
6131d95db74SKevin Wolf         readline_free(container_of(mon, MonitorHMP, common)->rs);
6141d95db74SKevin Wolf     }
615*20076f4aSMarkus Armbruster     g_string_free(mon->outbuf, true);
6161d95db74SKevin Wolf     qemu_mutex_destroy(&mon->mon_lock);
6171d95db74SKevin Wolf }
6181d95db74SKevin Wolf 
6191d95db74SKevin Wolf void monitor_cleanup(void)
6201d95db74SKevin Wolf {
6211d95db74SKevin Wolf     /*
6221d95db74SKevin Wolf      * We need to explicitly stop the I/O thread (but not destroy it),
6231d95db74SKevin Wolf      * clean up the monitor resources, then destroy the I/O thread since
6241d95db74SKevin Wolf      * we need to unregister from chardev below in
6251d95db74SKevin Wolf      * monitor_data_destroy(), and chardev is not thread-safe yet
6261d95db74SKevin Wolf      */
6271d95db74SKevin Wolf     if (mon_iothread) {
6281d95db74SKevin Wolf         iothread_stop(mon_iothread);
6291d95db74SKevin Wolf     }
6301d95db74SKevin Wolf 
6319ce44e2cSKevin Wolf     /*
632357bda95SKevin Wolf      * The dispatcher needs to stop before destroying the monitor and
633357bda95SKevin Wolf      * the I/O thread.
6349ce44e2cSKevin Wolf      *
6359ce44e2cSKevin Wolf      * We need to poll both qemu_aio_context and iohandler_ctx to make
6369ce44e2cSKevin Wolf      * sure that the dispatcher coroutine keeps making progress and
6379ce44e2cSKevin Wolf      * eventually terminates.  qemu_aio_context is automatically
6389ce44e2cSKevin Wolf      * polled by calling AIO_WAIT_WHILE on it, but we must poll
6399ce44e2cSKevin Wolf      * iohandler_ctx manually.
6409ce44e2cSKevin Wolf      */
6419ce44e2cSKevin Wolf     qmp_dispatcher_co_shutdown = true;
6429ce44e2cSKevin Wolf     if (!qatomic_xchg(&qmp_dispatcher_co_busy, true)) {
6439ce44e2cSKevin Wolf         aio_co_wake(qmp_dispatcher_co);
6449ce44e2cSKevin Wolf     }
6459ce44e2cSKevin Wolf 
6469ce44e2cSKevin Wolf     AIO_WAIT_WHILE(qemu_get_aio_context(),
6479ce44e2cSKevin Wolf                    (aio_poll(iohandler_get_aio_context(), false),
6489ce44e2cSKevin Wolf                     qatomic_mb_read(&qmp_dispatcher_co_busy)));
6499ce44e2cSKevin Wolf 
650357bda95SKevin Wolf     /* Flush output buffers and destroy monitors */
651357bda95SKevin Wolf     qemu_mutex_lock(&monitor_lock);
652357bda95SKevin Wolf     monitor_destroyed = true;
653357bda95SKevin Wolf     while (!QTAILQ_EMPTY(&mon_list)) {
654357bda95SKevin Wolf         Monitor *mon = QTAILQ_FIRST(&mon_list);
655357bda95SKevin Wolf         QTAILQ_REMOVE(&mon_list, mon, entry);
656357bda95SKevin Wolf         /* Permit QAPI event emission from character frontend release */
657357bda95SKevin Wolf         qemu_mutex_unlock(&monitor_lock);
658357bda95SKevin Wolf         monitor_flush(mon);
659357bda95SKevin Wolf         monitor_data_destroy(mon);
660357bda95SKevin Wolf         qemu_mutex_lock(&monitor_lock);
661357bda95SKevin Wolf         g_free(mon);
662357bda95SKevin Wolf     }
663357bda95SKevin Wolf     qemu_mutex_unlock(&monitor_lock);
664357bda95SKevin Wolf 
6651d95db74SKevin Wolf     if (mon_iothread) {
6661d95db74SKevin Wolf         iothread_destroy(mon_iothread);
6671d95db74SKevin Wolf         mon_iothread = NULL;
6681d95db74SKevin Wolf     }
6691d95db74SKevin Wolf }
6701d95db74SKevin Wolf 
6711d95db74SKevin Wolf static void monitor_qapi_event_init(void)
6721d95db74SKevin Wolf {
6731d95db74SKevin Wolf     monitor_qapi_event_state = g_hash_table_new(qapi_event_throttle_hash,
6741d95db74SKevin Wolf                                                 qapi_event_throttle_equal);
6751d95db74SKevin Wolf }
6761d95db74SKevin Wolf 
6771d95db74SKevin Wolf void monitor_init_globals_core(void)
6781d95db74SKevin Wolf {
6791d95db74SKevin Wolf     monitor_qapi_event_init();
6801d95db74SKevin Wolf     qemu_mutex_init(&monitor_lock);
681e69ee454SKevin Wolf     coroutine_mon = g_hash_table_new(NULL, NULL);
6821d95db74SKevin Wolf 
6831d95db74SKevin Wolf     /*
6841d95db74SKevin Wolf      * The dispatcher BH must run in the main loop thread, since we
6851d95db74SKevin Wolf      * have commands assuming that context.  It would be nice to get
6861d95db74SKevin Wolf      * rid of those assumptions.
6871d95db74SKevin Wolf      */
6889ce44e2cSKevin Wolf     qmp_dispatcher_co = qemu_coroutine_create(monitor_qmp_dispatcher_co, NULL);
6899ce44e2cSKevin Wolf     qatomic_mb_set(&qmp_dispatcher_co_busy, true);
6909ce44e2cSKevin Wolf     aio_co_schedule(iohandler_get_aio_context(), qmp_dispatcher_co);
6911d95db74SKevin Wolf }
6921d95db74SKevin Wolf 
693a2f411c4SKevin Wolf int monitor_init(MonitorOptions *opts, bool allow_hmp, Error **errp)
694c3e95551SKevin Wolf {
695c3e95551SKevin Wolf     Chardev *chr;
696f27a9bb3SKevin Wolf     Error *local_err = NULL;
697c3e95551SKevin Wolf 
698f2098725SKevin Wolf     chr = qemu_chr_find(opts->chardev);
699c3e95551SKevin Wolf     if (chr == NULL) {
700f2098725SKevin Wolf         error_setg(errp, "chardev \"%s\" not found", opts->chardev);
701c3e95551SKevin Wolf         return -1;
702c3e95551SKevin Wolf     }
703c3e95551SKevin Wolf 
704a2f411c4SKevin Wolf     if (!opts->has_mode) {
705a2f411c4SKevin Wolf         opts->mode = allow_hmp ? MONITOR_MODE_READLINE : MONITOR_MODE_CONTROL;
706a2f411c4SKevin Wolf     }
707a2f411c4SKevin Wolf 
708f2098725SKevin Wolf     switch (opts->mode) {
709f2098725SKevin Wolf     case MONITOR_MODE_CONTROL:
710f27a9bb3SKevin Wolf         monitor_init_qmp(chr, opts->pretty, &local_err);
711f2098725SKevin Wolf         break;
712f2098725SKevin Wolf     case MONITOR_MODE_READLINE:
713a2f411c4SKevin Wolf         if (!allow_hmp) {
714a2f411c4SKevin Wolf             error_setg(errp, "Only QMP is supported");
715a2f411c4SKevin Wolf             return -1;
716a2f411c4SKevin Wolf         }
717f2098725SKevin Wolf         if (opts->pretty) {
718f2098725SKevin Wolf             warn_report("'pretty' is deprecated for HMP monitors, it has no "
719f2098725SKevin Wolf                         "effect and will be removed in future versions");
720f2098725SKevin Wolf         }
7218e9119a8SKevin Wolf         monitor_init_hmp(chr, true, &local_err);
722f2098725SKevin Wolf         break;
723f2098725SKevin Wolf     default:
724f2098725SKevin Wolf         g_assert_not_reached();
725f2098725SKevin Wolf     }
726f2098725SKevin Wolf 
727f27a9bb3SKevin Wolf     if (local_err) {
728f27a9bb3SKevin Wolf         error_propagate(errp, local_err);
729f27a9bb3SKevin Wolf         return -1;
730f27a9bb3SKevin Wolf     }
731f2098725SKevin Wolf     return 0;
732f2098725SKevin Wolf }
733f2098725SKevin Wolf 
734f2098725SKevin Wolf int monitor_init_opts(QemuOpts *opts, Error **errp)
735f2098725SKevin Wolf {
736f2098725SKevin Wolf     Visitor *v;
737f2098725SKevin Wolf     MonitorOptions *options;
738b11a093cSMarkus Armbruster     int ret;
739f2098725SKevin Wolf 
740f2098725SKevin Wolf     v = opts_visitor_new(opts);
741b11a093cSMarkus Armbruster     visit_type_MonitorOptions(v, NULL, &options, errp);
742f2098725SKevin Wolf     visit_free(v);
743b11a093cSMarkus Armbruster     if (!options) {
744f2098725SKevin Wolf         return -1;
745c3e95551SKevin Wolf     }
746b11a093cSMarkus Armbruster 
747b11a093cSMarkus Armbruster     ret = monitor_init(options, true, errp);
748b11a093cSMarkus Armbruster     qapi_free_MonitorOptions(options);
749b11a093cSMarkus Armbruster     return ret;
750c3e95551SKevin Wolf }
751c3e95551SKevin Wolf 
7521d95db74SKevin Wolf QemuOptsList qemu_mon_opts = {
7531d95db74SKevin Wolf     .name = "mon",
7541d95db74SKevin Wolf     .implied_opt_name = "chardev",
7551d95db74SKevin Wolf     .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
7561d95db74SKevin Wolf     .desc = {
7571d95db74SKevin Wolf         {
7581d95db74SKevin Wolf             .name = "mode",
7591d95db74SKevin Wolf             .type = QEMU_OPT_STRING,
7601d95db74SKevin Wolf         },{
7611d95db74SKevin Wolf             .name = "chardev",
7621d95db74SKevin Wolf             .type = QEMU_OPT_STRING,
7631d95db74SKevin Wolf         },{
7641d95db74SKevin Wolf             .name = "pretty",
7651d95db74SKevin Wolf             .type = QEMU_OPT_BOOL,
7661d95db74SKevin Wolf         },
7671d95db74SKevin Wolf         { /* end of list */ }
7681d95db74SKevin Wolf     },
7691d95db74SKevin Wolf };
770