blob: ef68ca9d211345ffab4f4e7f54a12253ca62af2f [file] [log] [blame]
Kevin Wolf1d95db72019-06-13 17:34:02 +02001/*
2 * QEMU monitor
3 *
4 * Copyright (c) 2003-2004 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25#include "qemu/osdep.h"
26#include "monitor-internal.h"
27#include "qapi/error.h"
Kevin Wolff2098722020-02-24 15:30:04 +010028#include "qapi/opts-visitor.h"
Kevin Wolf1d95db72019-06-13 17:34:02 +020029#include "qapi/qapi-emit-events.h"
Kevin Wolff2098722020-02-24 15:30:04 +010030#include "qapi/qapi-visit-control.h"
Kevin Wolf1d95db72019-06-13 17:34:02 +020031#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"
Markus Armbrusterd5938f22019-08-12 07:23:56 +020036#include "sysemu/sysemu.h"
Kevin Wolf1d95db72019-06-13 17:34:02 +020037#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 */
44typedef 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
51typedef struct {
52 int64_t rate; /* Minimum time (in ns) between two events */
53} MonitorQAPIEventConf;
54
55/* Shared monitor I/O thread */
56IOThread *mon_iothread;
57
58/* Bottom half to dispatch the requests received from I/O thread */
59QEMUBH *qmp_dispatcher_bh;
60
Kevin Wolfe69ee452020-10-05 17:58:48 +020061/*
62 * Protects mon_list, monitor_qapi_event_state, coroutine_mon,
63 * monitor_destroyed.
64 */
Kevin Wolf1d95db72019-06-13 17:34:02 +020065QemuMutex monitor_lock;
66static GHashTable *monitor_qapi_event_state;
Kevin Wolfe69ee452020-10-05 17:58:48 +020067static GHashTable *coroutine_mon; /* Maps Coroutine* to Monitor* */
Kevin Wolf1d95db72019-06-13 17:34:02 +020068
69MonitorList mon_list;
70int mon_refcount;
71static bool monitor_destroyed;
72
Kevin Wolf947e4742020-10-05 17:58:44 +020073Monitor *monitor_cur(void)
74{
Kevin Wolfe69ee452020-10-05 17:58:48 +020075 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;
Kevin Wolf947e4742020-10-05 17:58:44 +020082}
83
84/**
85 * Sets a new current monitor and returns the old one.
Kevin Wolfe69ee452020-10-05 17:58:48 +020086 *
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.
Kevin Wolf947e4742020-10-05 17:58:44 +020090 */
Kevin Wolfe69ee452020-10-05 17:58:48 +020091Monitor *monitor_set_cur(Coroutine *co, Monitor *mon)
Kevin Wolf947e4742020-10-05 17:58:44 +020092{
Kevin Wolfe69ee452020-10-05 17:58:48 +020093 Monitor *old_monitor = monitor_cur();
Kevin Wolf947e4742020-10-05 17:58:44 +020094
Kevin Wolfe69ee452020-10-05 17:58:48 +020095 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
Kevin Wolf947e4742020-10-05 17:58:44 +0200103 return old_monitor;
104}
Kevin Wolf1d95db72019-06-13 17:34:02 +0200105
106/**
107 * Is the current monitor, if any, a QMP monitor?
108 */
109bool monitor_cur_is_qmp(void)
110{
Kevin Wolf947e4742020-10-05 17:58:44 +0200111 Monitor *cur_mon = monitor_cur();
112
Kevin Wolf1d95db72019-06-13 17:34:02 +0200113 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 */
Kevin Wolf92082412019-06-13 17:34:03 +0200121static inline bool monitor_uses_readline(const MonitorHMP *mon)
Kevin Wolf1d95db72019-06-13 17:34:02 +0200122{
Kevin Wolf92082412019-06-13 17:34:03 +0200123 return mon->use_readline;
Kevin Wolf1d95db72019-06-13 17:34:02 +0200124}
125
126static inline bool monitor_is_hmp_non_interactive(const Monitor *mon)
127{
Kevin Wolf92082412019-06-13 17:34:03 +0200128 if (monitor_is_qmp(mon)) {
129 return false;
130 }
131
132 return !monitor_uses_readline(container_of(mon, MonitorHMP, common));
Kevin Wolf1d95db72019-06-13 17:34:02 +0200133}
134
135static void monitor_flush_locked(Monitor *mon);
136
137static 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 */
150static 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
185void 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 */
193int 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
214int 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
233int 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 */
247int error_vprintf(const char *fmt, va_list ap)
248{
Kevin Wolf947e4742020-10-05 17:58:44 +0200249 Monitor *cur_mon = monitor_cur();
250
Kevin Wolf1d95db72019-06-13 17:34:02 +0200251 if (cur_mon && !monitor_cur_is_qmp()) {
252 return monitor_vprintf(cur_mon, fmt, ap);
253 }
254 return vfprintf(stderr, fmt, ap);
255}
256
257int error_vprintf_unless_qmp(const char *fmt, va_list ap)
258{
Kevin Wolf947e4742020-10-05 17:58:44 +0200259 Monitor *cur_mon = monitor_cur();
260
Kevin Wolf1d95db72019-06-13 17:34:02 +0200261 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
271static 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 },
David Hildenbrand722a3c72020-06-26 09:22:44 +0200279 [QAPI_EVENT_MEMORY_DEVICE_SIZE_CHANGE] = { 1000 * SCALE_MS },
Kevin Wolf1d95db72019-06-13 17:34:02 +0200280};
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 */
288static 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 */
298static 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
316static 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 */
322static void
323monitor_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
378void 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 */
424static 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
449static 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
465static 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
487int monitor_suspend(Monitor *mon)
488{
489 if (monitor_is_hmp_non_interactive(mon)) {
490 return -ENOTTY;
491 }
492
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100493 qatomic_inc(&mon->suspend_cnt);
Kevin Wolf1d95db72019-06-13 17:34:02 +0200494
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
507static void monitor_accept_input(void *opaque)
508{
509 Monitor *mon = opaque;
510
511 qemu_chr_fe_accept_input(&mon->chr);
512}
513
514void monitor_resume(Monitor *mon)
515{
516 if (monitor_is_hmp_non_interactive(mon)) {
517 return;
518 }
519
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100520 if (qatomic_dec_fetch(&mon->suspend_cnt) == 0) {
Kevin Wolf1d95db72019-06-13 17:34:02 +0200521 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
541int monitor_can_read(void *opaque)
542{
543 Monitor *mon = opaque;
544
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100545 return !qatomic_mb_read(&mon->suspend_cnt);
Kevin Wolf1d95db72019-06-13 17:34:02 +0200546}
547
548void 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
568static void monitor_iothread_init(void)
569{
570 mon_iothread = iothread_create("mon_iothread", &error_abort);
571}
572
Kevin Wolf92082412019-06-13 17:34:03 +0200573void monitor_data_init(Monitor *mon, bool is_qmp, bool skip_flush,
Kevin Wolf1d95db72019-06-13 17:34:02 +0200574 bool use_io_thread)
575{
576 if (use_io_thread && !mon_iothread) {
577 monitor_iothread_init();
578 }
579 qemu_mutex_init(&mon->mon_lock);
Kevin Wolf92082412019-06-13 17:34:03 +0200580 mon->is_qmp = is_qmp;
Kevin Wolf1d95db72019-06-13 17:34:02 +0200581 mon->outbuf = qstring_new();
582 mon->skip_flush = skip_flush;
583 mon->use_io_thread = use_io_thread;
Kevin Wolf1d95db72019-06-13 17:34:02 +0200584}
585
586void 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
Kevin Wolf1d95db72019-06-13 17:34:02 +0200599void 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
635static 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
641void monitor_init_globals_core(void)
642{
643 monitor_qapi_event_init();
644 qemu_mutex_init(&monitor_lock);
Kevin Wolfe69ee452020-10-05 17:58:48 +0200645 coroutine_mon = g_hash_table_new(NULL, NULL);
Kevin Wolf1d95db72019-06-13 17:34:02 +0200646
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
Kevin Wolfa2f411c2020-02-24 15:30:07 +0100657int monitor_init(MonitorOptions *opts, bool allow_hmp, Error **errp)
Kevin Wolfc3e95552020-01-29 11:22:36 +0100658{
659 Chardev *chr;
Kevin Wolff27a9bb2020-02-24 15:30:05 +0100660 Error *local_err = NULL;
Kevin Wolfc3e95552020-01-29 11:22:36 +0100661
Kevin Wolff2098722020-02-24 15:30:04 +0100662 chr = qemu_chr_find(opts->chardev);
Kevin Wolfc3e95552020-01-29 11:22:36 +0100663 if (chr == NULL) {
Kevin Wolff2098722020-02-24 15:30:04 +0100664 error_setg(errp, "chardev \"%s\" not found", opts->chardev);
Kevin Wolfc3e95552020-01-29 11:22:36 +0100665 return -1;
666 }
667
Kevin Wolfa2f411c2020-02-24 15:30:07 +0100668 if (!opts->has_mode) {
669 opts->mode = allow_hmp ? MONITOR_MODE_READLINE : MONITOR_MODE_CONTROL;
670 }
671
Kevin Wolff2098722020-02-24 15:30:04 +0100672 switch (opts->mode) {
673 case MONITOR_MODE_CONTROL:
Kevin Wolff27a9bb2020-02-24 15:30:05 +0100674 monitor_init_qmp(chr, opts->pretty, &local_err);
Kevin Wolff2098722020-02-24 15:30:04 +0100675 break;
676 case MONITOR_MODE_READLINE:
Kevin Wolfa2f411c2020-02-24 15:30:07 +0100677 if (!allow_hmp) {
678 error_setg(errp, "Only QMP is supported");
679 return -1;
680 }
Kevin Wolff2098722020-02-24 15:30:04 +0100681 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 }
Kevin Wolf8e9119a2020-02-24 15:30:06 +0100685 monitor_init_hmp(chr, true, &local_err);
Kevin Wolff2098722020-02-24 15:30:04 +0100686 break;
687 default:
688 g_assert_not_reached();
689 }
690
Kevin Wolff27a9bb2020-02-24 15:30:05 +0100691 if (local_err) {
692 error_propagate(errp, local_err);
693 return -1;
694 }
Kevin Wolff2098722020-02-24 15:30:04 +0100695 return 0;
696}
697
698int monitor_init_opts(QemuOpts *opts, Error **errp)
699{
700 Visitor *v;
701 MonitorOptions *options;
Markus Armbrusterb11a0932020-07-07 18:06:07 +0200702 int ret;
Kevin Wolff2098722020-02-24 15:30:04 +0100703
704 v = opts_visitor_new(opts);
Markus Armbrusterb11a0932020-07-07 18:06:07 +0200705 visit_type_MonitorOptions(v, NULL, &options, errp);
Kevin Wolff2098722020-02-24 15:30:04 +0100706 visit_free(v);
Markus Armbrusterb11a0932020-07-07 18:06:07 +0200707 if (!options) {
Kevin Wolff2098722020-02-24 15:30:04 +0100708 return -1;
Kevin Wolfc3e95552020-01-29 11:22:36 +0100709 }
Markus Armbrusterb11a0932020-07-07 18:06:07 +0200710
711 ret = monitor_init(options, true, errp);
712 qapi_free_MonitorOptions(options);
713 return ret;
Kevin Wolfc3e95552020-01-29 11:22:36 +0100714}
715
Kevin Wolf1d95db72019-06-13 17:34:02 +0200716QemuOptsList 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};