xref: /openbmc/qemu/monitor/monitor.c (revision e69ee454)
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"
28 #include "qapi/opts-visitor.h"
29 #include "qapi/qapi-emit-events.h"
30 #include "qapi/qapi-visit-control.h"
31 #include "qapi/qmp/qdict.h"
32 #include "qapi/qmp/qstring.h"
33 #include "qemu/error-report.h"
34 #include "qemu/option.h"
35 #include "sysemu/qtest.h"
36 #include "sysemu/sysemu.h"
37 #include "trace.h"
38 
39 /*
40  * To prevent flooding clients, events can be throttled. The
41  * throttling is calculated globally, rather than per-Monitor
42  * instance.
43  */
44 typedef struct MonitorQAPIEventState {
45     QAPIEvent event;    /* Throttling state for this event type and... */
46     QDict *data;        /* ... data, see qapi_event_throttle_equal() */
47     QEMUTimer *timer;   /* Timer for handling delayed events */
48     QDict *qdict;       /* Delayed event (if any) */
49 } MonitorQAPIEventState;
50 
51 typedef struct {
52     int64_t rate;       /* Minimum time (in ns) between two events */
53 } MonitorQAPIEventConf;
54 
55 /* Shared monitor I/O thread */
56 IOThread *mon_iothread;
57 
58 /* Bottom half to dispatch the requests received from I/O thread */
59 QEMUBH *qmp_dispatcher_bh;
60 
61 /*
62  * Protects mon_list, monitor_qapi_event_state, coroutine_mon,
63  * monitor_destroyed.
64  */
65 QemuMutex monitor_lock;
66 static GHashTable *monitor_qapi_event_state;
67 static GHashTable *coroutine_mon; /* Maps Coroutine* to Monitor* */
68 
69 MonitorList mon_list;
70 int mon_refcount;
71 static bool monitor_destroyed;
72 
73 Monitor *monitor_cur(void)
74 {
75     Monitor *mon;
76 
77     qemu_mutex_lock(&monitor_lock);
78     mon = g_hash_table_lookup(coroutine_mon, qemu_coroutine_self());
79     qemu_mutex_unlock(&monitor_lock);
80 
81     return mon;
82 }
83 
84 /**
85  * Sets a new current monitor and returns the old one.
86  *
87  * If a non-NULL monitor is set for a coroutine, another call
88  * resetting it to NULL is required before the coroutine terminates,
89  * otherwise a stale entry would remain in the hash table.
90  */
91 Monitor *monitor_set_cur(Coroutine *co, Monitor *mon)
92 {
93     Monitor *old_monitor = monitor_cur();
94 
95     qemu_mutex_lock(&monitor_lock);
96     if (mon) {
97         g_hash_table_replace(coroutine_mon, co, mon);
98     } else {
99         g_hash_table_remove(coroutine_mon, co);
100     }
101     qemu_mutex_unlock(&monitor_lock);
102 
103     return old_monitor;
104 }
105 
106 /**
107  * Is the current monitor, if any, a QMP monitor?
108  */
109 bool monitor_cur_is_qmp(void)
110 {
111     Monitor *cur_mon = monitor_cur();
112 
113     return cur_mon && monitor_is_qmp(cur_mon);
114 }
115 
116 /**
117  * Is @mon is using readline?
118  * Note: not all HMP monitors use readline, e.g., gdbserver has a
119  * non-interactive HMP monitor, so readline is not used there.
120  */
121 static inline bool monitor_uses_readline(const MonitorHMP *mon)
122 {
123     return mon->use_readline;
124 }
125 
126 static inline bool monitor_is_hmp_non_interactive(const Monitor *mon)
127 {
128     if (monitor_is_qmp(mon)) {
129         return false;
130     }
131 
132     return !monitor_uses_readline(container_of(mon, MonitorHMP, common));
133 }
134 
135 static void monitor_flush_locked(Monitor *mon);
136 
137 static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
138                                   void *opaque)
139 {
140     Monitor *mon = opaque;
141 
142     qemu_mutex_lock(&mon->mon_lock);
143     mon->out_watch = 0;
144     monitor_flush_locked(mon);
145     qemu_mutex_unlock(&mon->mon_lock);
146     return FALSE;
147 }
148 
149 /* Caller must hold mon->mon_lock */
150 static void monitor_flush_locked(Monitor *mon)
151 {
152     int rc;
153     size_t len;
154     const char *buf;
155 
156     if (mon->skip_flush) {
157         return;
158     }
159 
160     buf = qstring_get_str(mon->outbuf);
161     len = qstring_get_length(mon->outbuf);
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 */
167             qobject_unref(mon->outbuf);
168             mon->outbuf = qstring_new();
169             return;
170         }
171         if (rc > 0) {
172             /* partial write */
173             QString *tmp = qstring_from_str(buf + rc);
174             qobject_unref(mon->outbuf);
175             mon->outbuf = tmp;
176         }
177         if (mon->out_watch == 0) {
178             mon->out_watch =
179                 qemu_chr_fe_add_watch(&mon->chr, G_IO_OUT | G_IO_HUP,
180                                       monitor_unblocked, mon);
181         }
182     }
183 }
184 
185 void monitor_flush(Monitor *mon)
186 {
187     qemu_mutex_lock(&mon->mon_lock);
188     monitor_flush_locked(mon);
189     qemu_mutex_unlock(&mon->mon_lock);
190 }
191 
192 /* flush at every end of line */
193 int monitor_puts(Monitor *mon, const char *str)
194 {
195     int i;
196     char c;
197 
198     qemu_mutex_lock(&mon->mon_lock);
199     for (i = 0; str[i]; i++) {
200         c = str[i];
201         if (c == '\n') {
202             qstring_append_chr(mon->outbuf, '\r');
203         }
204         qstring_append_chr(mon->outbuf, c);
205         if (c == '\n') {
206             monitor_flush_locked(mon);
207         }
208     }
209     qemu_mutex_unlock(&mon->mon_lock);
210 
211     return i;
212 }
213 
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 
244 /*
245  * Print to current monitor if we have one, else to stderr.
246  */
247 int error_vprintf(const char *fmt, va_list ap)
248 {
249     Monitor *cur_mon = monitor_cur();
250 
251     if (cur_mon && !monitor_cur_is_qmp()) {
252         return monitor_vprintf(cur_mon, fmt, ap);
253     }
254     return vfprintf(stderr, fmt, ap);
255 }
256 
257 int error_vprintf_unless_qmp(const char *fmt, va_list ap)
258 {
259     Monitor *cur_mon = monitor_cur();
260 
261     if (!cur_mon) {
262         return vfprintf(stderr, fmt, ap);
263     }
264     if (!monitor_cur_is_qmp()) {
265         return monitor_vprintf(cur_mon, fmt, ap);
266     }
267     return -1;
268 }
269 
270 
271 static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = {
272     /* Limit guest-triggerable events to 1 per second */
273     [QAPI_EVENT_RTC_CHANGE]        = { 1000 * SCALE_MS },
274     [QAPI_EVENT_WATCHDOG]          = { 1000 * SCALE_MS },
275     [QAPI_EVENT_BALLOON_CHANGE]    = { 1000 * SCALE_MS },
276     [QAPI_EVENT_QUORUM_REPORT_BAD] = { 1000 * SCALE_MS },
277     [QAPI_EVENT_QUORUM_FAILURE]    = { 1000 * SCALE_MS },
278     [QAPI_EVENT_VSERPORT_CHANGE]   = { 1000 * SCALE_MS },
279     [QAPI_EVENT_MEMORY_DEVICE_SIZE_CHANGE] = { 1000 * SCALE_MS },
280 };
281 
282 /*
283  * Return the clock to use for recording an event's time.
284  * It's QEMU_CLOCK_REALTIME, except for qtests it's
285  * QEMU_CLOCK_VIRTUAL, to support testing rate limits.
286  * Beware: result is invalid before configure_accelerator().
287  */
288 static inline QEMUClockType monitor_get_event_clock(void)
289 {
290     return qtest_enabled() ? QEMU_CLOCK_VIRTUAL : QEMU_CLOCK_REALTIME;
291 }
292 
293 /*
294  * Broadcast an event to all monitors.
295  * @qdict is the event object.  Its member "event" must match @event.
296  * Caller must hold monitor_lock.
297  */
298 static void monitor_qapi_event_emit(QAPIEvent event, QDict *qdict)
299 {
300     Monitor *mon;
301     MonitorQMP *qmp_mon;
302 
303     trace_monitor_protocol_event_emit(event, qdict);
304     QTAILQ_FOREACH(mon, &mon_list, entry) {
305         if (!monitor_is_qmp(mon)) {
306             continue;
307         }
308 
309         qmp_mon = container_of(mon, MonitorQMP, common);
310         if (qmp_mon->commands != &qmp_cap_negotiation_commands) {
311             qmp_send_response(qmp_mon, qdict);
312         }
313     }
314 }
315 
316 static void monitor_qapi_event_handler(void *opaque);
317 
318 /*
319  * Queue a new event for emission to Monitor instances,
320  * applying any rate limiting if required.
321  */
322 static void
323 monitor_qapi_event_queue_no_reenter(QAPIEvent event, QDict *qdict)
324 {
325     MonitorQAPIEventConf *evconf;
326     MonitorQAPIEventState *evstate;
327 
328     assert(event < QAPI_EVENT__MAX);
329     evconf = &monitor_qapi_event_conf[event];
330     trace_monitor_protocol_event_queue(event, qdict, evconf->rate);
331 
332     qemu_mutex_lock(&monitor_lock);
333 
334     if (!evconf->rate) {
335         /* Unthrottled event */
336         monitor_qapi_event_emit(event, qdict);
337     } else {
338         QDict *data = qobject_to(QDict, qdict_get(qdict, "data"));
339         MonitorQAPIEventState key = { .event = event, .data = data };
340 
341         evstate = g_hash_table_lookup(monitor_qapi_event_state, &key);
342         assert(!evstate || timer_pending(evstate->timer));
343 
344         if (evstate) {
345             /*
346              * Timer is pending for (at least) evconf->rate ns after
347              * last send.  Store event for sending when timer fires,
348              * replacing a prior stored event if any.
349              */
350             qobject_unref(evstate->qdict);
351             evstate->qdict = qobject_ref(qdict);
352         } else {
353             /*
354              * Last send was (at least) evconf->rate ns ago.
355              * Send immediately, and arm the timer to call
356              * monitor_qapi_event_handler() in evconf->rate ns.  Any
357              * events arriving before then will be delayed until then.
358              */
359             int64_t now = qemu_clock_get_ns(monitor_get_event_clock());
360 
361             monitor_qapi_event_emit(event, qdict);
362 
363             evstate = g_new(MonitorQAPIEventState, 1);
364             evstate->event = event;
365             evstate->data = qobject_ref(data);
366             evstate->qdict = NULL;
367             evstate->timer = timer_new_ns(monitor_get_event_clock(),
368                                           monitor_qapi_event_handler,
369                                           evstate);
370             g_hash_table_add(monitor_qapi_event_state, evstate);
371             timer_mod_ns(evstate->timer, now + evconf->rate);
372         }
373     }
374 
375     qemu_mutex_unlock(&monitor_lock);
376 }
377 
378 void qapi_event_emit(QAPIEvent event, QDict *qdict)
379 {
380     /*
381      * monitor_qapi_event_queue_no_reenter() is not reentrant: it
382      * would deadlock on monitor_lock.  Work around by queueing
383      * events in thread-local storage.
384      * TODO: remove this, make it re-enter safe.
385      */
386     typedef struct MonitorQapiEvent {
387         QAPIEvent event;
388         QDict *qdict;
389         QSIMPLEQ_ENTRY(MonitorQapiEvent) entry;
390     } MonitorQapiEvent;
391     static __thread QSIMPLEQ_HEAD(, MonitorQapiEvent) event_queue;
392     static __thread bool reentered;
393     MonitorQapiEvent *ev;
394 
395     if (!reentered) {
396         QSIMPLEQ_INIT(&event_queue);
397     }
398 
399     ev = g_new(MonitorQapiEvent, 1);
400     ev->qdict = qobject_ref(qdict);
401     ev->event = event;
402     QSIMPLEQ_INSERT_TAIL(&event_queue, ev, entry);
403     if (reentered) {
404         return;
405     }
406 
407     reentered = true;
408 
409     while ((ev = QSIMPLEQ_FIRST(&event_queue)) != NULL) {
410         QSIMPLEQ_REMOVE_HEAD(&event_queue, entry);
411         monitor_qapi_event_queue_no_reenter(ev->event, ev->qdict);
412         qobject_unref(ev->qdict);
413         g_free(ev);
414     }
415 
416     reentered = false;
417 }
418 
419 /*
420  * This function runs evconf->rate ns after sending a throttled
421  * event.
422  * If another event has since been stored, send it.
423  */
424 static void monitor_qapi_event_handler(void *opaque)
425 {
426     MonitorQAPIEventState *evstate = opaque;
427     MonitorQAPIEventConf *evconf = &monitor_qapi_event_conf[evstate->event];
428 
429     trace_monitor_protocol_event_handler(evstate->event, evstate->qdict);
430     qemu_mutex_lock(&monitor_lock);
431 
432     if (evstate->qdict) {
433         int64_t now = qemu_clock_get_ns(monitor_get_event_clock());
434 
435         monitor_qapi_event_emit(evstate->event, evstate->qdict);
436         qobject_unref(evstate->qdict);
437         evstate->qdict = NULL;
438         timer_mod_ns(evstate->timer, now + evconf->rate);
439     } else {
440         g_hash_table_remove(monitor_qapi_event_state, evstate);
441         qobject_unref(evstate->data);
442         timer_free(evstate->timer);
443         g_free(evstate);
444     }
445 
446     qemu_mutex_unlock(&monitor_lock);
447 }
448 
449 static unsigned int qapi_event_throttle_hash(const void *key)
450 {
451     const MonitorQAPIEventState *evstate = key;
452     unsigned int hash = evstate->event * 255;
453 
454     if (evstate->event == QAPI_EVENT_VSERPORT_CHANGE) {
455         hash += g_str_hash(qdict_get_str(evstate->data, "id"));
456     }
457 
458     if (evstate->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
459         hash += g_str_hash(qdict_get_str(evstate->data, "node-name"));
460     }
461 
462     return hash;
463 }
464 
465 static gboolean qapi_event_throttle_equal(const void *a, const void *b)
466 {
467     const MonitorQAPIEventState *eva = a;
468     const MonitorQAPIEventState *evb = b;
469 
470     if (eva->event != evb->event) {
471         return FALSE;
472     }
473 
474     if (eva->event == QAPI_EVENT_VSERPORT_CHANGE) {
475         return !strcmp(qdict_get_str(eva->data, "id"),
476                        qdict_get_str(evb->data, "id"));
477     }
478 
479     if (eva->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
480         return !strcmp(qdict_get_str(eva->data, "node-name"),
481                        qdict_get_str(evb->data, "node-name"));
482     }
483 
484     return TRUE;
485 }
486 
487 int monitor_suspend(Monitor *mon)
488 {
489     if (monitor_is_hmp_non_interactive(mon)) {
490         return -ENOTTY;
491     }
492 
493     qatomic_inc(&mon->suspend_cnt);
494 
495     if (mon->use_io_thread) {
496         /*
497          * Kick I/O thread to make sure this takes effect.  It'll be
498          * evaluated again in prepare() of the watch object.
499          */
500         aio_notify(iothread_get_aio_context(mon_iothread));
501     }
502 
503     trace_monitor_suspend(mon, 1);
504     return 0;
505 }
506 
507 static void monitor_accept_input(void *opaque)
508 {
509     Monitor *mon = opaque;
510 
511     qemu_chr_fe_accept_input(&mon->chr);
512 }
513 
514 void monitor_resume(Monitor *mon)
515 {
516     if (monitor_is_hmp_non_interactive(mon)) {
517         return;
518     }
519 
520     if (qatomic_dec_fetch(&mon->suspend_cnt) == 0) {
521         AioContext *ctx;
522 
523         if (mon->use_io_thread) {
524             ctx = iothread_get_aio_context(mon_iothread);
525         } else {
526             ctx = qemu_get_aio_context();
527         }
528 
529         if (!monitor_is_qmp(mon)) {
530             MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common);
531             assert(hmp_mon->rs);
532             readline_show_prompt(hmp_mon->rs);
533         }
534 
535         aio_bh_schedule_oneshot(ctx, monitor_accept_input, mon);
536     }
537 
538     trace_monitor_suspend(mon, -1);
539 }
540 
541 int monitor_can_read(void *opaque)
542 {
543     Monitor *mon = opaque;
544 
545     return !qatomic_mb_read(&mon->suspend_cnt);
546 }
547 
548 void monitor_list_append(Monitor *mon)
549 {
550     qemu_mutex_lock(&monitor_lock);
551     /*
552      * This prevents inserting new monitors during monitor_cleanup().
553      * A cleaner solution would involve the main thread telling other
554      * threads to terminate, waiting for their termination.
555      */
556     if (!monitor_destroyed) {
557         QTAILQ_INSERT_HEAD(&mon_list, mon, entry);
558         mon = NULL;
559     }
560     qemu_mutex_unlock(&monitor_lock);
561 
562     if (mon) {
563         monitor_data_destroy(mon);
564         g_free(mon);
565     }
566 }
567 
568 static void monitor_iothread_init(void)
569 {
570     mon_iothread = iothread_create("mon_iothread", &error_abort);
571 }
572 
573 void monitor_data_init(Monitor *mon, bool is_qmp, bool skip_flush,
574                        bool use_io_thread)
575 {
576     if (use_io_thread && !mon_iothread) {
577         monitor_iothread_init();
578     }
579     qemu_mutex_init(&mon->mon_lock);
580     mon->is_qmp = is_qmp;
581     mon->outbuf = qstring_new();
582     mon->skip_flush = skip_flush;
583     mon->use_io_thread = use_io_thread;
584 }
585 
586 void monitor_data_destroy(Monitor *mon)
587 {
588     g_free(mon->mon_cpu_path);
589     qemu_chr_fe_deinit(&mon->chr, false);
590     if (monitor_is_qmp(mon)) {
591         monitor_data_destroy_qmp(container_of(mon, MonitorQMP, common));
592     } else {
593         readline_free(container_of(mon, MonitorHMP, common)->rs);
594     }
595     qobject_unref(mon->outbuf);
596     qemu_mutex_destroy(&mon->mon_lock);
597 }
598 
599 void monitor_cleanup(void)
600 {
601     /*
602      * We need to explicitly stop the I/O thread (but not destroy it),
603      * clean up the monitor resources, then destroy the I/O thread since
604      * we need to unregister from chardev below in
605      * monitor_data_destroy(), and chardev is not thread-safe yet
606      */
607     if (mon_iothread) {
608         iothread_stop(mon_iothread);
609     }
610 
611     /* Flush output buffers and destroy monitors */
612     qemu_mutex_lock(&monitor_lock);
613     monitor_destroyed = true;
614     while (!QTAILQ_EMPTY(&mon_list)) {
615         Monitor *mon = QTAILQ_FIRST(&mon_list);
616         QTAILQ_REMOVE(&mon_list, mon, entry);
617         /* Permit QAPI event emission from character frontend release */
618         qemu_mutex_unlock(&monitor_lock);
619         monitor_flush(mon);
620         monitor_data_destroy(mon);
621         qemu_mutex_lock(&monitor_lock);
622         g_free(mon);
623     }
624     qemu_mutex_unlock(&monitor_lock);
625 
626     /* QEMUBHs needs to be deleted before destroying the I/O thread */
627     qemu_bh_delete(qmp_dispatcher_bh);
628     qmp_dispatcher_bh = NULL;
629     if (mon_iothread) {
630         iothread_destroy(mon_iothread);
631         mon_iothread = NULL;
632     }
633 }
634 
635 static void monitor_qapi_event_init(void)
636 {
637     monitor_qapi_event_state = g_hash_table_new(qapi_event_throttle_hash,
638                                                 qapi_event_throttle_equal);
639 }
640 
641 void monitor_init_globals_core(void)
642 {
643     monitor_qapi_event_init();
644     qemu_mutex_init(&monitor_lock);
645     coroutine_mon = g_hash_table_new(NULL, NULL);
646 
647     /*
648      * The dispatcher BH must run in the main loop thread, since we
649      * have commands assuming that context.  It would be nice to get
650      * rid of those assumptions.
651      */
652     qmp_dispatcher_bh = aio_bh_new(iohandler_get_aio_context(),
653                                    monitor_qmp_bh_dispatcher,
654                                    NULL);
655 }
656 
657 int monitor_init(MonitorOptions *opts, bool allow_hmp, Error **errp)
658 {
659     Chardev *chr;
660     Error *local_err = NULL;
661 
662     chr = qemu_chr_find(opts->chardev);
663     if (chr == NULL) {
664         error_setg(errp, "chardev \"%s\" not found", opts->chardev);
665         return -1;
666     }
667 
668     if (!opts->has_mode) {
669         opts->mode = allow_hmp ? MONITOR_MODE_READLINE : MONITOR_MODE_CONTROL;
670     }
671 
672     switch (opts->mode) {
673     case MONITOR_MODE_CONTROL:
674         monitor_init_qmp(chr, opts->pretty, &local_err);
675         break;
676     case MONITOR_MODE_READLINE:
677         if (!allow_hmp) {
678             error_setg(errp, "Only QMP is supported");
679             return -1;
680         }
681         if (opts->pretty) {
682             warn_report("'pretty' is deprecated for HMP monitors, it has no "
683                         "effect and will be removed in future versions");
684         }
685         monitor_init_hmp(chr, true, &local_err);
686         break;
687     default:
688         g_assert_not_reached();
689     }
690 
691     if (local_err) {
692         error_propagate(errp, local_err);
693         return -1;
694     }
695     return 0;
696 }
697 
698 int monitor_init_opts(QemuOpts *opts, Error **errp)
699 {
700     Visitor *v;
701     MonitorOptions *options;
702     int ret;
703 
704     v = opts_visitor_new(opts);
705     visit_type_MonitorOptions(v, NULL, &options, errp);
706     visit_free(v);
707     if (!options) {
708         return -1;
709     }
710 
711     ret = monitor_init(options, true, errp);
712     qapi_free_MonitorOptions(options);
713     return ret;
714 }
715 
716 QemuOptsList qemu_mon_opts = {
717     .name = "mon",
718     .implied_opt_name = "chardev",
719     .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
720     .desc = {
721         {
722             .name = "mode",
723             .type = QEMU_OPT_STRING,
724         },{
725             .name = "chardev",
726             .type = QEMU_OPT_STRING,
727         },{
728             .name = "pretty",
729             .type = QEMU_OPT_BOOL,
730         },
731         { /* end of list */ }
732     },
733 };
734