meson: use pkg-config method for libudev
[qemu/ar7.git] / monitor / monitor.c
blob1e4a6b3f20a16007ba49dbca63a25634a5de8c1f
1 /*
2 * QEMU monitor
4 * Copyright (c) 2003-2004 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
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 "qemu/error-report.h"
33 #include "qemu/option.h"
34 #include "sysemu/qtest.h"
35 #include "sysemu/sysemu.h"
36 #include "trace.h"
39 * To prevent flooding clients, events can be throttled. The
40 * throttling is calculated globally, rather than per-Monitor
41 * instance.
43 typedef struct MonitorQAPIEventState {
44 QAPIEvent event; /* Throttling state for this event type and... */
45 QDict *data; /* ... data, see qapi_event_throttle_equal() */
46 QEMUTimer *timer; /* Timer for handling delayed events */
47 QDict *qdict; /* Delayed event (if any) */
48 } MonitorQAPIEventState;
50 typedef struct {
51 int64_t rate; /* Minimum time (in ns) between two events */
52 } MonitorQAPIEventConf;
54 /* Shared monitor I/O thread */
55 IOThread *mon_iothread;
57 /* Coroutine to dispatch the requests received from I/O thread */
58 Coroutine *qmp_dispatcher_co;
60 /* Set to true when the dispatcher coroutine should terminate */
61 bool qmp_dispatcher_co_shutdown;
64 * qmp_dispatcher_co_busy is used for synchronisation between the
65 * monitor thread and the main thread to ensure that the dispatcher
66 * coroutine never gets scheduled a second time when it's already
67 * scheduled (scheduling the same coroutine twice is forbidden).
69 * It is true if the coroutine is active and processing requests.
70 * Additional requests may then be pushed onto mon->qmp_requests,
71 * and @qmp_dispatcher_co_shutdown may be set without further ado.
72 * @qmp_dispatcher_co_busy must not be woken up in this case.
74 * If false, you also have to set @qmp_dispatcher_co_busy to true and
75 * wake up @qmp_dispatcher_co after pushing the new requests.
77 * The coroutine will automatically change this variable back to false
78 * before it yields. Nobody else may set the variable to false.
80 * Access must be atomic for thread safety.
82 bool qmp_dispatcher_co_busy;
85 * Protects mon_list, monitor_qapi_event_state, coroutine_mon,
86 * monitor_destroyed.
88 QemuMutex monitor_lock;
89 static GHashTable *monitor_qapi_event_state;
90 static GHashTable *coroutine_mon; /* Maps Coroutine* to Monitor* */
92 MonitorList mon_list;
93 int mon_refcount;
94 static bool monitor_destroyed;
96 Monitor *monitor_cur(void)
98 Monitor *mon;
100 qemu_mutex_lock(&monitor_lock);
101 mon = g_hash_table_lookup(coroutine_mon, qemu_coroutine_self());
102 qemu_mutex_unlock(&monitor_lock);
104 return mon;
108 * Sets a new current monitor and returns the old one.
110 * If a non-NULL monitor is set for a coroutine, another call
111 * resetting it to NULL is required before the coroutine terminates,
112 * otherwise a stale entry would remain in the hash table.
114 Monitor *monitor_set_cur(Coroutine *co, Monitor *mon)
116 Monitor *old_monitor = monitor_cur();
118 qemu_mutex_lock(&monitor_lock);
119 if (mon) {
120 g_hash_table_replace(coroutine_mon, co, mon);
121 } else {
122 g_hash_table_remove(coroutine_mon, co);
124 qemu_mutex_unlock(&monitor_lock);
126 return old_monitor;
130 * Is the current monitor, if any, a QMP monitor?
132 bool monitor_cur_is_qmp(void)
134 Monitor *cur_mon = monitor_cur();
136 return cur_mon && monitor_is_qmp(cur_mon);
140 * Is @mon is using readline?
141 * Note: not all HMP monitors use readline, e.g., gdbserver has a
142 * non-interactive HMP monitor, so readline is not used there.
144 static inline bool monitor_uses_readline(const MonitorHMP *mon)
146 return mon->use_readline;
149 static inline bool monitor_is_hmp_non_interactive(const Monitor *mon)
151 if (monitor_is_qmp(mon)) {
152 return false;
155 return !monitor_uses_readline(container_of(mon, MonitorHMP, common));
158 static void monitor_flush_locked(Monitor *mon);
160 static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
161 void *opaque)
163 Monitor *mon = opaque;
165 qemu_mutex_lock(&mon->mon_lock);
166 mon->out_watch = 0;
167 monitor_flush_locked(mon);
168 qemu_mutex_unlock(&mon->mon_lock);
169 return FALSE;
172 /* Caller must hold mon->mon_lock */
173 static void monitor_flush_locked(Monitor *mon)
175 int rc;
176 size_t len;
177 const char *buf;
179 if (mon->skip_flush) {
180 return;
183 buf = mon->outbuf->str;
184 len = mon->outbuf->len;
186 if (len && !mon->mux_out) {
187 rc = qemu_chr_fe_write(&mon->chr, (const uint8_t *) buf, len);
188 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
189 /* all flushed or error */
190 g_string_truncate(mon->outbuf, 0);
191 return;
193 if (rc > 0) {
194 /* partial write */
195 g_string_erase(mon->outbuf, 0, rc);
197 if (mon->out_watch == 0) {
198 mon->out_watch =
199 qemu_chr_fe_add_watch(&mon->chr, G_IO_OUT | G_IO_HUP,
200 monitor_unblocked, mon);
205 void monitor_flush(Monitor *mon)
207 qemu_mutex_lock(&mon->mon_lock);
208 monitor_flush_locked(mon);
209 qemu_mutex_unlock(&mon->mon_lock);
212 /* flush at every end of line */
213 int monitor_puts(Monitor *mon, const char *str)
215 int i;
216 char c;
218 qemu_mutex_lock(&mon->mon_lock);
219 for (i = 0; str[i]; i++) {
220 c = str[i];
221 if (c == '\n') {
222 g_string_append_c(mon->outbuf, '\r');
224 g_string_append_c(mon->outbuf, c);
225 if (c == '\n') {
226 monitor_flush_locked(mon);
229 qemu_mutex_unlock(&mon->mon_lock);
231 return i;
234 int monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
236 char *buf;
237 int n;
239 if (!mon) {
240 return -1;
243 if (monitor_is_qmp(mon)) {
244 return -1;
247 buf = g_strdup_vprintf(fmt, ap);
248 n = monitor_puts(mon, buf);
249 g_free(buf);
250 return n;
253 int monitor_printf(Monitor *mon, const char *fmt, ...)
255 int ret;
257 va_list ap;
258 va_start(ap, fmt);
259 ret = monitor_vprintf(mon, fmt, ap);
260 va_end(ap);
261 return ret;
265 * Print to current monitor if we have one, else to stderr.
267 int error_vprintf(const char *fmt, va_list ap)
269 Monitor *cur_mon = monitor_cur();
271 if (cur_mon && !monitor_cur_is_qmp()) {
272 return monitor_vprintf(cur_mon, fmt, ap);
274 return vfprintf(stderr, fmt, ap);
277 int error_vprintf_unless_qmp(const char *fmt, va_list ap)
279 Monitor *cur_mon = monitor_cur();
281 if (!cur_mon) {
282 return vfprintf(stderr, fmt, ap);
284 if (!monitor_cur_is_qmp()) {
285 return monitor_vprintf(cur_mon, fmt, ap);
287 return -1;
291 static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = {
292 /* Limit guest-triggerable events to 1 per second */
293 [QAPI_EVENT_RTC_CHANGE] = { 1000 * SCALE_MS },
294 [QAPI_EVENT_WATCHDOG] = { 1000 * SCALE_MS },
295 [QAPI_EVENT_BALLOON_CHANGE] = { 1000 * SCALE_MS },
296 [QAPI_EVENT_QUORUM_REPORT_BAD] = { 1000 * SCALE_MS },
297 [QAPI_EVENT_QUORUM_FAILURE] = { 1000 * SCALE_MS },
298 [QAPI_EVENT_VSERPORT_CHANGE] = { 1000 * SCALE_MS },
299 [QAPI_EVENT_MEMORY_DEVICE_SIZE_CHANGE] = { 1000 * SCALE_MS },
303 * Return the clock to use for recording an event's time.
304 * It's QEMU_CLOCK_REALTIME, except for qtests it's
305 * QEMU_CLOCK_VIRTUAL, to support testing rate limits.
306 * Beware: result is invalid before configure_accelerator().
308 static inline QEMUClockType monitor_get_event_clock(void)
310 return qtest_enabled() ? QEMU_CLOCK_VIRTUAL : QEMU_CLOCK_REALTIME;
314 * Broadcast an event to all monitors.
315 * @qdict is the event object. Its member "event" must match @event.
316 * Caller must hold monitor_lock.
318 static void monitor_qapi_event_emit(QAPIEvent event, QDict *qdict)
320 Monitor *mon;
321 MonitorQMP *qmp_mon;
323 trace_monitor_protocol_event_emit(event, qdict);
324 QTAILQ_FOREACH(mon, &mon_list, entry) {
325 if (!monitor_is_qmp(mon)) {
326 continue;
329 qmp_mon = container_of(mon, MonitorQMP, common);
330 if (qmp_mon->commands != &qmp_cap_negotiation_commands) {
331 qmp_send_response(qmp_mon, qdict);
336 static void monitor_qapi_event_handler(void *opaque);
339 * Queue a new event for emission to Monitor instances,
340 * applying any rate limiting if required.
342 static void
343 monitor_qapi_event_queue_no_reenter(QAPIEvent event, QDict *qdict)
345 MonitorQAPIEventConf *evconf;
346 MonitorQAPIEventState *evstate;
348 assert(event < QAPI_EVENT__MAX);
349 evconf = &monitor_qapi_event_conf[event];
350 trace_monitor_protocol_event_queue(event, qdict, evconf->rate);
352 qemu_mutex_lock(&monitor_lock);
354 if (!evconf->rate) {
355 /* Unthrottled event */
356 monitor_qapi_event_emit(event, qdict);
357 } else {
358 QDict *data = qobject_to(QDict, qdict_get(qdict, "data"));
359 MonitorQAPIEventState key = { .event = event, .data = data };
361 evstate = g_hash_table_lookup(monitor_qapi_event_state, &key);
362 assert(!evstate || timer_pending(evstate->timer));
364 if (evstate) {
366 * Timer is pending for (at least) evconf->rate ns after
367 * last send. Store event for sending when timer fires,
368 * replacing a prior stored event if any.
370 qobject_unref(evstate->qdict);
371 evstate->qdict = qobject_ref(qdict);
372 } else {
374 * Last send was (at least) evconf->rate ns ago.
375 * Send immediately, and arm the timer to call
376 * monitor_qapi_event_handler() in evconf->rate ns. Any
377 * events arriving before then will be delayed until then.
379 int64_t now = qemu_clock_get_ns(monitor_get_event_clock());
381 monitor_qapi_event_emit(event, qdict);
383 evstate = g_new(MonitorQAPIEventState, 1);
384 evstate->event = event;
385 evstate->data = qobject_ref(data);
386 evstate->qdict = NULL;
387 evstate->timer = timer_new_ns(monitor_get_event_clock(),
388 monitor_qapi_event_handler,
389 evstate);
390 g_hash_table_add(monitor_qapi_event_state, evstate);
391 timer_mod_ns(evstate->timer, now + evconf->rate);
395 qemu_mutex_unlock(&monitor_lock);
398 void qapi_event_emit(QAPIEvent event, QDict *qdict)
401 * monitor_qapi_event_queue_no_reenter() is not reentrant: it
402 * would deadlock on monitor_lock. Work around by queueing
403 * events in thread-local storage.
404 * TODO: remove this, make it re-enter safe.
406 typedef struct MonitorQapiEvent {
407 QAPIEvent event;
408 QDict *qdict;
409 QSIMPLEQ_ENTRY(MonitorQapiEvent) entry;
410 } MonitorQapiEvent;
411 static __thread QSIMPLEQ_HEAD(, MonitorQapiEvent) event_queue;
412 static __thread bool reentered;
413 MonitorQapiEvent *ev;
415 if (!reentered) {
416 QSIMPLEQ_INIT(&event_queue);
419 ev = g_new(MonitorQapiEvent, 1);
420 ev->qdict = qobject_ref(qdict);
421 ev->event = event;
422 QSIMPLEQ_INSERT_TAIL(&event_queue, ev, entry);
423 if (reentered) {
424 return;
427 reentered = true;
429 while ((ev = QSIMPLEQ_FIRST(&event_queue)) != NULL) {
430 QSIMPLEQ_REMOVE_HEAD(&event_queue, entry);
431 monitor_qapi_event_queue_no_reenter(ev->event, ev->qdict);
432 qobject_unref(ev->qdict);
433 g_free(ev);
436 reentered = false;
440 * This function runs evconf->rate ns after sending a throttled
441 * event.
442 * If another event has since been stored, send it.
444 static void monitor_qapi_event_handler(void *opaque)
446 MonitorQAPIEventState *evstate = opaque;
447 MonitorQAPIEventConf *evconf = &monitor_qapi_event_conf[evstate->event];
449 trace_monitor_protocol_event_handler(evstate->event, evstate->qdict);
450 qemu_mutex_lock(&monitor_lock);
452 if (evstate->qdict) {
453 int64_t now = qemu_clock_get_ns(monitor_get_event_clock());
455 monitor_qapi_event_emit(evstate->event, evstate->qdict);
456 qobject_unref(evstate->qdict);
457 evstate->qdict = NULL;
458 timer_mod_ns(evstate->timer, now + evconf->rate);
459 } else {
460 g_hash_table_remove(monitor_qapi_event_state, evstate);
461 qobject_unref(evstate->data);
462 timer_free(evstate->timer);
463 g_free(evstate);
466 qemu_mutex_unlock(&monitor_lock);
469 static unsigned int qapi_event_throttle_hash(const void *key)
471 const MonitorQAPIEventState *evstate = key;
472 unsigned int hash = evstate->event * 255;
474 if (evstate->event == QAPI_EVENT_VSERPORT_CHANGE) {
475 hash += g_str_hash(qdict_get_str(evstate->data, "id"));
478 if (evstate->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
479 hash += g_str_hash(qdict_get_str(evstate->data, "node-name"));
482 return hash;
485 static gboolean qapi_event_throttle_equal(const void *a, const void *b)
487 const MonitorQAPIEventState *eva = a;
488 const MonitorQAPIEventState *evb = b;
490 if (eva->event != evb->event) {
491 return FALSE;
494 if (eva->event == QAPI_EVENT_VSERPORT_CHANGE) {
495 return !strcmp(qdict_get_str(eva->data, "id"),
496 qdict_get_str(evb->data, "id"));
499 if (eva->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
500 return !strcmp(qdict_get_str(eva->data, "node-name"),
501 qdict_get_str(evb->data, "node-name"));
504 return TRUE;
507 int monitor_suspend(Monitor *mon)
509 if (monitor_is_hmp_non_interactive(mon)) {
510 return -ENOTTY;
513 qatomic_inc(&mon->suspend_cnt);
515 if (mon->use_io_thread) {
517 * Kick I/O thread to make sure this takes effect. It'll be
518 * evaluated again in prepare() of the watch object.
520 aio_notify(iothread_get_aio_context(mon_iothread));
523 trace_monitor_suspend(mon, 1);
524 return 0;
527 static void monitor_accept_input(void *opaque)
529 Monitor *mon = opaque;
531 qemu_chr_fe_accept_input(&mon->chr);
534 void monitor_resume(Monitor *mon)
536 if (monitor_is_hmp_non_interactive(mon)) {
537 return;
540 if (qatomic_dec_fetch(&mon->suspend_cnt) == 0) {
541 AioContext *ctx;
543 if (mon->use_io_thread) {
544 ctx = iothread_get_aio_context(mon_iothread);
545 } else {
546 ctx = qemu_get_aio_context();
549 if (!monitor_is_qmp(mon)) {
550 MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common);
551 assert(hmp_mon->rs);
552 readline_show_prompt(hmp_mon->rs);
555 aio_bh_schedule_oneshot(ctx, monitor_accept_input, mon);
558 trace_monitor_suspend(mon, -1);
561 int monitor_can_read(void *opaque)
563 Monitor *mon = opaque;
565 return !qatomic_mb_read(&mon->suspend_cnt);
568 void monitor_list_append(Monitor *mon)
570 qemu_mutex_lock(&monitor_lock);
572 * This prevents inserting new monitors during monitor_cleanup().
573 * A cleaner solution would involve the main thread telling other
574 * threads to terminate, waiting for their termination.
576 if (!monitor_destroyed) {
577 QTAILQ_INSERT_HEAD(&mon_list, mon, entry);
578 mon = NULL;
580 qemu_mutex_unlock(&monitor_lock);
582 if (mon) {
583 monitor_data_destroy(mon);
584 g_free(mon);
588 static void monitor_iothread_init(void)
590 mon_iothread = iothread_create("mon_iothread", &error_abort);
593 void monitor_data_init(Monitor *mon, bool is_qmp, bool skip_flush,
594 bool use_io_thread)
596 if (use_io_thread && !mon_iothread) {
597 monitor_iothread_init();
599 qemu_mutex_init(&mon->mon_lock);
600 mon->is_qmp = is_qmp;
601 mon->outbuf = g_string_new(NULL);
602 mon->skip_flush = skip_flush;
603 mon->use_io_thread = use_io_thread;
606 void monitor_data_destroy(Monitor *mon)
608 g_free(mon->mon_cpu_path);
609 qemu_chr_fe_deinit(&mon->chr, false);
610 if (monitor_is_qmp(mon)) {
611 monitor_data_destroy_qmp(container_of(mon, MonitorQMP, common));
612 } else {
613 readline_free(container_of(mon, MonitorHMP, common)->rs);
615 g_string_free(mon->outbuf, true);
616 qemu_mutex_destroy(&mon->mon_lock);
619 void monitor_cleanup(void)
622 * We need to explicitly stop the I/O thread (but not destroy it),
623 * clean up the monitor resources, then destroy the I/O thread since
624 * we need to unregister from chardev below in
625 * monitor_data_destroy(), and chardev is not thread-safe yet
627 if (mon_iothread) {
628 iothread_stop(mon_iothread);
632 * The dispatcher needs to stop before destroying the monitor and
633 * the I/O thread.
635 * We need to poll both qemu_aio_context and iohandler_ctx to make
636 * sure that the dispatcher coroutine keeps making progress and
637 * eventually terminates. qemu_aio_context is automatically
638 * polled by calling AIO_WAIT_WHILE on it, but we must poll
639 * iohandler_ctx manually.
641 qmp_dispatcher_co_shutdown = true;
642 if (!qatomic_xchg(&qmp_dispatcher_co_busy, true)) {
643 aio_co_wake(qmp_dispatcher_co);
646 AIO_WAIT_WHILE(qemu_get_aio_context(),
647 (aio_poll(iohandler_get_aio_context(), false),
648 qatomic_mb_read(&qmp_dispatcher_co_busy)));
650 /* Flush output buffers and destroy monitors */
651 qemu_mutex_lock(&monitor_lock);
652 monitor_destroyed = true;
653 while (!QTAILQ_EMPTY(&mon_list)) {
654 Monitor *mon = QTAILQ_FIRST(&mon_list);
655 QTAILQ_REMOVE(&mon_list, mon, entry);
656 /* Permit QAPI event emission from character frontend release */
657 qemu_mutex_unlock(&monitor_lock);
658 monitor_flush(mon);
659 monitor_data_destroy(mon);
660 qemu_mutex_lock(&monitor_lock);
661 g_free(mon);
663 qemu_mutex_unlock(&monitor_lock);
665 if (mon_iothread) {
666 iothread_destroy(mon_iothread);
667 mon_iothread = NULL;
671 static void monitor_qapi_event_init(void)
673 monitor_qapi_event_state = g_hash_table_new(qapi_event_throttle_hash,
674 qapi_event_throttle_equal);
677 void monitor_init_globals_core(void)
679 monitor_qapi_event_init();
680 qemu_mutex_init(&monitor_lock);
681 coroutine_mon = g_hash_table_new(NULL, NULL);
684 * The dispatcher BH must run in the main loop thread, since we
685 * have commands assuming that context. It would be nice to get
686 * rid of those assumptions.
688 qmp_dispatcher_co = qemu_coroutine_create(monitor_qmp_dispatcher_co, NULL);
689 qatomic_mb_set(&qmp_dispatcher_co_busy, true);
690 aio_co_schedule(iohandler_get_aio_context(), qmp_dispatcher_co);
693 int monitor_init(MonitorOptions *opts, bool allow_hmp, Error **errp)
695 Chardev *chr;
696 Error *local_err = NULL;
698 chr = qemu_chr_find(opts->chardev);
699 if (chr == NULL) {
700 error_setg(errp, "chardev \"%s\" not found", opts->chardev);
701 return -1;
704 if (!opts->has_mode) {
705 opts->mode = allow_hmp ? MONITOR_MODE_READLINE : MONITOR_MODE_CONTROL;
708 switch (opts->mode) {
709 case MONITOR_MODE_CONTROL:
710 monitor_init_qmp(chr, opts->pretty, &local_err);
711 break;
712 case MONITOR_MODE_READLINE:
713 if (!allow_hmp) {
714 error_setg(errp, "Only QMP is supported");
715 return -1;
717 if (opts->pretty) {
718 warn_report("'pretty' is deprecated for HMP monitors, it has no "
719 "effect and will be removed in future versions");
721 monitor_init_hmp(chr, true, &local_err);
722 break;
723 default:
724 g_assert_not_reached();
727 if (local_err) {
728 error_propagate(errp, local_err);
729 return -1;
731 return 0;
734 int monitor_init_opts(QemuOpts *opts, Error **errp)
736 Visitor *v;
737 MonitorOptions *options;
738 int ret;
740 v = opts_visitor_new(opts);
741 visit_type_MonitorOptions(v, NULL, &options, errp);
742 visit_free(v);
743 if (!options) {
744 return -1;
747 ret = monitor_init(options, true, errp);
748 qapi_free_MonitorOptions(options);
749 return ret;
752 QemuOptsList qemu_mon_opts = {
753 .name = "mon",
754 .implied_opt_name = "chardev",
755 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
756 .desc = {
758 .name = "mode",
759 .type = QEMU_OPT_STRING,
761 .name = "chardev",
762 .type = QEMU_OPT_STRING,
764 .name = "pretty",
765 .type = QEMU_OPT_BOOL,
767 { /* end of list */ }