migration: Make explicit that we are quitting multifd
[qemu/ar7.git] / monitor / monitor.c
blob3ef28171c0dc6b79962527561be1fa51ecc8db9b
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/qapi-emit-events.h"
29 #include "qapi/qmp/qdict.h"
30 #include "qapi/qmp/qstring.h"
31 #include "qemu/error-report.h"
32 #include "qemu/option.h"
33 #include "sysemu/qtest.h"
34 #include "trace.h"
37 * To prevent flooding clients, events can be throttled. The
38 * throttling is calculated globally, rather than per-Monitor
39 * instance.
41 typedef struct MonitorQAPIEventState {
42 QAPIEvent event; /* Throttling state for this event type and... */
43 QDict *data; /* ... data, see qapi_event_throttle_equal() */
44 QEMUTimer *timer; /* Timer for handling delayed events */
45 QDict *qdict; /* Delayed event (if any) */
46 } MonitorQAPIEventState;
48 typedef struct {
49 int64_t rate; /* Minimum time (in ns) between two events */
50 } MonitorQAPIEventConf;
52 /* Shared monitor I/O thread */
53 IOThread *mon_iothread;
55 /* Bottom half to dispatch the requests received from I/O thread */
56 QEMUBH *qmp_dispatcher_bh;
58 /* Protects mon_list, monitor_qapi_event_state, monitor_destroyed. */
59 QemuMutex monitor_lock;
60 static GHashTable *monitor_qapi_event_state;
62 MonitorList mon_list;
63 int mon_refcount;
64 static bool monitor_destroyed;
66 __thread Monitor *cur_mon;
68 /**
69 * Is the current monitor, if any, a QMP monitor?
71 bool monitor_cur_is_qmp(void)
73 return cur_mon && monitor_is_qmp(cur_mon);
76 /**
77 * Is @mon is using readline?
78 * Note: not all HMP monitors use readline, e.g., gdbserver has a
79 * non-interactive HMP monitor, so readline is not used there.
81 static inline bool monitor_uses_readline(const MonitorHMP *mon)
83 return mon->use_readline;
86 static inline bool monitor_is_hmp_non_interactive(const Monitor *mon)
88 if (monitor_is_qmp(mon)) {
89 return false;
92 return !monitor_uses_readline(container_of(mon, MonitorHMP, common));
95 static void monitor_flush_locked(Monitor *mon);
97 static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
98 void *opaque)
100 Monitor *mon = opaque;
102 qemu_mutex_lock(&mon->mon_lock);
103 mon->out_watch = 0;
104 monitor_flush_locked(mon);
105 qemu_mutex_unlock(&mon->mon_lock);
106 return FALSE;
109 /* Caller must hold mon->mon_lock */
110 static void monitor_flush_locked(Monitor *mon)
112 int rc;
113 size_t len;
114 const char *buf;
116 if (mon->skip_flush) {
117 return;
120 buf = qstring_get_str(mon->outbuf);
121 len = qstring_get_length(mon->outbuf);
123 if (len && !mon->mux_out) {
124 rc = qemu_chr_fe_write(&mon->chr, (const uint8_t *) buf, len);
125 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
126 /* all flushed or error */
127 qobject_unref(mon->outbuf);
128 mon->outbuf = qstring_new();
129 return;
131 if (rc > 0) {
132 /* partial write */
133 QString *tmp = qstring_from_str(buf + rc);
134 qobject_unref(mon->outbuf);
135 mon->outbuf = tmp;
137 if (mon->out_watch == 0) {
138 mon->out_watch =
139 qemu_chr_fe_add_watch(&mon->chr, G_IO_OUT | G_IO_HUP,
140 monitor_unblocked, mon);
145 void monitor_flush(Monitor *mon)
147 qemu_mutex_lock(&mon->mon_lock);
148 monitor_flush_locked(mon);
149 qemu_mutex_unlock(&mon->mon_lock);
152 /* flush at every end of line */
153 int monitor_puts(Monitor *mon, const char *str)
155 int i;
156 char c;
158 qemu_mutex_lock(&mon->mon_lock);
159 for (i = 0; str[i]; i++) {
160 c = str[i];
161 if (c == '\n') {
162 qstring_append_chr(mon->outbuf, '\r');
164 qstring_append_chr(mon->outbuf, c);
165 if (c == '\n') {
166 monitor_flush_locked(mon);
169 qemu_mutex_unlock(&mon->mon_lock);
171 return i;
174 int monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
176 char *buf;
177 int n;
179 if (!mon) {
180 return -1;
183 if (monitor_is_qmp(mon)) {
184 return -1;
187 buf = g_strdup_vprintf(fmt, ap);
188 n = monitor_puts(mon, buf);
189 g_free(buf);
190 return n;
193 int monitor_printf(Monitor *mon, const char *fmt, ...)
195 int ret;
197 va_list ap;
198 va_start(ap, fmt);
199 ret = monitor_vprintf(mon, fmt, ap);
200 va_end(ap);
201 return ret;
205 * Print to current monitor if we have one, else to stderr.
207 int error_vprintf(const char *fmt, va_list ap)
209 if (cur_mon && !monitor_cur_is_qmp()) {
210 return monitor_vprintf(cur_mon, fmt, ap);
212 return vfprintf(stderr, fmt, ap);
215 int error_vprintf_unless_qmp(const char *fmt, va_list ap)
217 if (!cur_mon) {
218 return vfprintf(stderr, fmt, ap);
220 if (!monitor_cur_is_qmp()) {
221 return monitor_vprintf(cur_mon, fmt, ap);
223 return -1;
227 static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = {
228 /* Limit guest-triggerable events to 1 per second */
229 [QAPI_EVENT_RTC_CHANGE] = { 1000 * SCALE_MS },
230 [QAPI_EVENT_WATCHDOG] = { 1000 * SCALE_MS },
231 [QAPI_EVENT_BALLOON_CHANGE] = { 1000 * SCALE_MS },
232 [QAPI_EVENT_QUORUM_REPORT_BAD] = { 1000 * SCALE_MS },
233 [QAPI_EVENT_QUORUM_FAILURE] = { 1000 * SCALE_MS },
234 [QAPI_EVENT_VSERPORT_CHANGE] = { 1000 * SCALE_MS },
238 * Return the clock to use for recording an event's time.
239 * It's QEMU_CLOCK_REALTIME, except for qtests it's
240 * QEMU_CLOCK_VIRTUAL, to support testing rate limits.
241 * Beware: result is invalid before configure_accelerator().
243 static inline QEMUClockType monitor_get_event_clock(void)
245 return qtest_enabled() ? QEMU_CLOCK_VIRTUAL : QEMU_CLOCK_REALTIME;
249 * Broadcast an event to all monitors.
250 * @qdict is the event object. Its member "event" must match @event.
251 * Caller must hold monitor_lock.
253 static void monitor_qapi_event_emit(QAPIEvent event, QDict *qdict)
255 Monitor *mon;
256 MonitorQMP *qmp_mon;
258 trace_monitor_protocol_event_emit(event, qdict);
259 QTAILQ_FOREACH(mon, &mon_list, entry) {
260 if (!monitor_is_qmp(mon)) {
261 continue;
264 qmp_mon = container_of(mon, MonitorQMP, common);
265 if (qmp_mon->commands != &qmp_cap_negotiation_commands) {
266 qmp_send_response(qmp_mon, qdict);
271 static void monitor_qapi_event_handler(void *opaque);
274 * Queue a new event for emission to Monitor instances,
275 * applying any rate limiting if required.
277 static void
278 monitor_qapi_event_queue_no_reenter(QAPIEvent event, QDict *qdict)
280 MonitorQAPIEventConf *evconf;
281 MonitorQAPIEventState *evstate;
283 assert(event < QAPI_EVENT__MAX);
284 evconf = &monitor_qapi_event_conf[event];
285 trace_monitor_protocol_event_queue(event, qdict, evconf->rate);
287 qemu_mutex_lock(&monitor_lock);
289 if (!evconf->rate) {
290 /* Unthrottled event */
291 monitor_qapi_event_emit(event, qdict);
292 } else {
293 QDict *data = qobject_to(QDict, qdict_get(qdict, "data"));
294 MonitorQAPIEventState key = { .event = event, .data = data };
296 evstate = g_hash_table_lookup(monitor_qapi_event_state, &key);
297 assert(!evstate || timer_pending(evstate->timer));
299 if (evstate) {
301 * Timer is pending for (at least) evconf->rate ns after
302 * last send. Store event for sending when timer fires,
303 * replacing a prior stored event if any.
305 qobject_unref(evstate->qdict);
306 evstate->qdict = qobject_ref(qdict);
307 } else {
309 * Last send was (at least) evconf->rate ns ago.
310 * Send immediately, and arm the timer to call
311 * monitor_qapi_event_handler() in evconf->rate ns. Any
312 * events arriving before then will be delayed until then.
314 int64_t now = qemu_clock_get_ns(monitor_get_event_clock());
316 monitor_qapi_event_emit(event, qdict);
318 evstate = g_new(MonitorQAPIEventState, 1);
319 evstate->event = event;
320 evstate->data = qobject_ref(data);
321 evstate->qdict = NULL;
322 evstate->timer = timer_new_ns(monitor_get_event_clock(),
323 monitor_qapi_event_handler,
324 evstate);
325 g_hash_table_add(monitor_qapi_event_state, evstate);
326 timer_mod_ns(evstate->timer, now + evconf->rate);
330 qemu_mutex_unlock(&monitor_lock);
333 void qapi_event_emit(QAPIEvent event, QDict *qdict)
336 * monitor_qapi_event_queue_no_reenter() is not reentrant: it
337 * would deadlock on monitor_lock. Work around by queueing
338 * events in thread-local storage.
339 * TODO: remove this, make it re-enter safe.
341 typedef struct MonitorQapiEvent {
342 QAPIEvent event;
343 QDict *qdict;
344 QSIMPLEQ_ENTRY(MonitorQapiEvent) entry;
345 } MonitorQapiEvent;
346 static __thread QSIMPLEQ_HEAD(, MonitorQapiEvent) event_queue;
347 static __thread bool reentered;
348 MonitorQapiEvent *ev;
350 if (!reentered) {
351 QSIMPLEQ_INIT(&event_queue);
354 ev = g_new(MonitorQapiEvent, 1);
355 ev->qdict = qobject_ref(qdict);
356 ev->event = event;
357 QSIMPLEQ_INSERT_TAIL(&event_queue, ev, entry);
358 if (reentered) {
359 return;
362 reentered = true;
364 while ((ev = QSIMPLEQ_FIRST(&event_queue)) != NULL) {
365 QSIMPLEQ_REMOVE_HEAD(&event_queue, entry);
366 monitor_qapi_event_queue_no_reenter(ev->event, ev->qdict);
367 qobject_unref(ev->qdict);
368 g_free(ev);
371 reentered = false;
375 * This function runs evconf->rate ns after sending a throttled
376 * event.
377 * If another event has since been stored, send it.
379 static void monitor_qapi_event_handler(void *opaque)
381 MonitorQAPIEventState *evstate = opaque;
382 MonitorQAPIEventConf *evconf = &monitor_qapi_event_conf[evstate->event];
384 trace_monitor_protocol_event_handler(evstate->event, evstate->qdict);
385 qemu_mutex_lock(&monitor_lock);
387 if (evstate->qdict) {
388 int64_t now = qemu_clock_get_ns(monitor_get_event_clock());
390 monitor_qapi_event_emit(evstate->event, evstate->qdict);
391 qobject_unref(evstate->qdict);
392 evstate->qdict = NULL;
393 timer_mod_ns(evstate->timer, now + evconf->rate);
394 } else {
395 g_hash_table_remove(monitor_qapi_event_state, evstate);
396 qobject_unref(evstate->data);
397 timer_free(evstate->timer);
398 g_free(evstate);
401 qemu_mutex_unlock(&monitor_lock);
404 static unsigned int qapi_event_throttle_hash(const void *key)
406 const MonitorQAPIEventState *evstate = key;
407 unsigned int hash = evstate->event * 255;
409 if (evstate->event == QAPI_EVENT_VSERPORT_CHANGE) {
410 hash += g_str_hash(qdict_get_str(evstate->data, "id"));
413 if (evstate->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
414 hash += g_str_hash(qdict_get_str(evstate->data, "node-name"));
417 return hash;
420 static gboolean qapi_event_throttle_equal(const void *a, const void *b)
422 const MonitorQAPIEventState *eva = a;
423 const MonitorQAPIEventState *evb = b;
425 if (eva->event != evb->event) {
426 return FALSE;
429 if (eva->event == QAPI_EVENT_VSERPORT_CHANGE) {
430 return !strcmp(qdict_get_str(eva->data, "id"),
431 qdict_get_str(evb->data, "id"));
434 if (eva->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
435 return !strcmp(qdict_get_str(eva->data, "node-name"),
436 qdict_get_str(evb->data, "node-name"));
439 return TRUE;
442 int monitor_suspend(Monitor *mon)
444 if (monitor_is_hmp_non_interactive(mon)) {
445 return -ENOTTY;
448 atomic_inc(&mon->suspend_cnt);
450 if (mon->use_io_thread) {
452 * Kick I/O thread to make sure this takes effect. It'll be
453 * evaluated again in prepare() of the watch object.
455 aio_notify(iothread_get_aio_context(mon_iothread));
458 trace_monitor_suspend(mon, 1);
459 return 0;
462 static void monitor_accept_input(void *opaque)
464 Monitor *mon = opaque;
466 qemu_chr_fe_accept_input(&mon->chr);
469 void monitor_resume(Monitor *mon)
471 if (monitor_is_hmp_non_interactive(mon)) {
472 return;
475 if (atomic_dec_fetch(&mon->suspend_cnt) == 0) {
476 AioContext *ctx;
478 if (mon->use_io_thread) {
479 ctx = iothread_get_aio_context(mon_iothread);
480 } else {
481 ctx = qemu_get_aio_context();
484 if (!monitor_is_qmp(mon)) {
485 MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common);
486 assert(hmp_mon->rs);
487 readline_show_prompt(hmp_mon->rs);
490 aio_bh_schedule_oneshot(ctx, monitor_accept_input, mon);
493 trace_monitor_suspend(mon, -1);
496 int monitor_can_read(void *opaque)
498 Monitor *mon = opaque;
500 return !atomic_mb_read(&mon->suspend_cnt);
503 void monitor_list_append(Monitor *mon)
505 qemu_mutex_lock(&monitor_lock);
507 * This prevents inserting new monitors during monitor_cleanup().
508 * A cleaner solution would involve the main thread telling other
509 * threads to terminate, waiting for their termination.
511 if (!monitor_destroyed) {
512 QTAILQ_INSERT_HEAD(&mon_list, mon, entry);
513 mon = NULL;
515 qemu_mutex_unlock(&monitor_lock);
517 if (mon) {
518 monitor_data_destroy(mon);
519 g_free(mon);
523 static void monitor_iothread_init(void)
525 mon_iothread = iothread_create("mon_iothread", &error_abort);
528 void monitor_data_init(Monitor *mon, bool is_qmp, bool skip_flush,
529 bool use_io_thread)
531 if (use_io_thread && !mon_iothread) {
532 monitor_iothread_init();
534 qemu_mutex_init(&mon->mon_lock);
535 mon->is_qmp = is_qmp;
536 mon->outbuf = qstring_new();
537 mon->skip_flush = skip_flush;
538 mon->use_io_thread = use_io_thread;
541 void monitor_data_destroy(Monitor *mon)
543 g_free(mon->mon_cpu_path);
544 qemu_chr_fe_deinit(&mon->chr, false);
545 if (monitor_is_qmp(mon)) {
546 monitor_data_destroy_qmp(container_of(mon, MonitorQMP, common));
547 } else {
548 readline_free(container_of(mon, MonitorHMP, common)->rs);
550 qobject_unref(mon->outbuf);
551 qemu_mutex_destroy(&mon->mon_lock);
554 void monitor_cleanup(void)
557 * We need to explicitly stop the I/O thread (but not destroy it),
558 * clean up the monitor resources, then destroy the I/O thread since
559 * we need to unregister from chardev below in
560 * monitor_data_destroy(), and chardev is not thread-safe yet
562 if (mon_iothread) {
563 iothread_stop(mon_iothread);
566 /* Flush output buffers and destroy monitors */
567 qemu_mutex_lock(&monitor_lock);
568 monitor_destroyed = true;
569 while (!QTAILQ_EMPTY(&mon_list)) {
570 Monitor *mon = QTAILQ_FIRST(&mon_list);
571 QTAILQ_REMOVE(&mon_list, mon, entry);
572 /* Permit QAPI event emission from character frontend release */
573 qemu_mutex_unlock(&monitor_lock);
574 monitor_flush(mon);
575 monitor_data_destroy(mon);
576 qemu_mutex_lock(&monitor_lock);
577 g_free(mon);
579 qemu_mutex_unlock(&monitor_lock);
581 /* QEMUBHs needs to be deleted before destroying the I/O thread */
582 qemu_bh_delete(qmp_dispatcher_bh);
583 qmp_dispatcher_bh = NULL;
584 if (mon_iothread) {
585 iothread_destroy(mon_iothread);
586 mon_iothread = NULL;
590 static void monitor_qapi_event_init(void)
592 monitor_qapi_event_state = g_hash_table_new(qapi_event_throttle_hash,
593 qapi_event_throttle_equal);
596 void monitor_init_globals_core(void)
598 monitor_qapi_event_init();
599 qemu_mutex_init(&monitor_lock);
602 * The dispatcher BH must run in the main loop thread, since we
603 * have commands assuming that context. It would be nice to get
604 * rid of those assumptions.
606 qmp_dispatcher_bh = aio_bh_new(iohandler_get_aio_context(),
607 monitor_qmp_bh_dispatcher,
608 NULL);
611 QemuOptsList qemu_mon_opts = {
612 .name = "mon",
613 .implied_opt_name = "chardev",
614 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
615 .desc = {
617 .name = "mode",
618 .type = QEMU_OPT_STRING,
620 .name = "chardev",
621 .type = QEMU_OPT_STRING,
623 .name = "pretty",
624 .type = QEMU_OPT_BOOL,
626 { /* end of list */ }