Lines Matching +full:0 +full:- +full:mon
4 * Copyright (c) 2003-2004 Fabrice Bellard
26 #include "monitor-internal.h"
28 #include "qapi/opts-visitor.h"
29 #include "qapi/qapi-emit-events.h"
30 #include "qapi/qapi-visit-control.h"
32 #include "qemu/error-report.h"
39 * throttling is calculated globally, rather than per-Monitor
78 Monitor *mon; in monitor_cur() local
81 mon = g_hash_table_lookup(coroutine_mon, qemu_coroutine_self()); in monitor_cur()
84 return mon; in monitor_cur()
90 * If a non-NULL monitor is set for a coroutine, another call
94 Monitor *monitor_set_cur(Coroutine *co, Monitor *mon) in monitor_set_cur() argument
99 if (mon) { in monitor_set_cur()
100 g_hash_table_replace(coroutine_mon, co, mon); in monitor_set_cur()
120 * Is @mon is using readline?
122 * non-interactive HMP monitor, so readline is not used there.
124 static inline bool monitor_uses_readline(const MonitorHMP *mon) in monitor_uses_readline() argument
126 return mon->use_readline; in monitor_uses_readline()
129 static inline bool monitor_is_hmp_non_interactive(const Monitor *mon) in monitor_is_hmp_non_interactive() argument
131 if (monitor_is_qmp(mon)) { in monitor_is_hmp_non_interactive()
135 return !monitor_uses_readline(container_of(mon, MonitorHMP, common)); in monitor_is_hmp_non_interactive()
141 Monitor *mon = opaque; in monitor_unblocked() local
143 QEMU_LOCK_GUARD(&mon->mon_lock); in monitor_unblocked()
144 mon->out_watch = 0; in monitor_unblocked()
145 monitor_flush_locked(mon); in monitor_unblocked()
149 /* Caller must hold mon->mon_lock */
150 void monitor_flush_locked(Monitor *mon) in monitor_flush_locked() argument
156 if (mon->skip_flush) { in monitor_flush_locked()
160 buf = mon->outbuf->str; in monitor_flush_locked()
161 len = mon->outbuf->len; in monitor_flush_locked()
163 if (len && !mon->mux_out) { in monitor_flush_locked()
164 rc = qemu_chr_fe_write(&mon->chr, (const uint8_t *) buf, len); in monitor_flush_locked()
165 if ((rc < 0 && errno != EAGAIN) || (rc == len)) { in monitor_flush_locked()
167 g_string_truncate(mon->outbuf, 0); in monitor_flush_locked()
170 if (rc > 0) { in monitor_flush_locked()
172 g_string_erase(mon->outbuf, 0, rc); in monitor_flush_locked()
174 if (mon->out_watch == 0) { in monitor_flush_locked()
175 mon->out_watch = in monitor_flush_locked()
176 qemu_chr_fe_add_watch(&mon->chr, G_IO_OUT | G_IO_HUP, in monitor_flush_locked()
177 monitor_unblocked, mon); in monitor_flush_locked()
182 void monitor_flush(Monitor *mon) in monitor_flush() argument
184 QEMU_LOCK_GUARD(&mon->mon_lock); in monitor_flush()
185 monitor_flush_locked(mon); in monitor_flush()
189 int monitor_puts_locked(Monitor *mon, const char *str) in monitor_puts_locked() argument
194 for (i = 0; str[i]; i++) { in monitor_puts_locked()
197 g_string_append_c(mon->outbuf, '\r'); in monitor_puts_locked()
199 g_string_append_c(mon->outbuf, c); in monitor_puts_locked()
201 monitor_flush_locked(mon); in monitor_puts_locked()
208 int monitor_puts(Monitor *mon, const char *str) in monitor_puts() argument
210 QEMU_LOCK_GUARD(&mon->mon_lock); in monitor_puts()
211 return monitor_puts_locked(mon, str); in monitor_puts()
214 int monitor_vprintf(Monitor *mon, const char *fmt, va_list ap) in monitor_vprintf() argument
219 if (!mon) { in monitor_vprintf()
220 return -1; in monitor_vprintf()
223 if (monitor_is_qmp(mon)) { in monitor_vprintf()
224 return -1; in monitor_vprintf()
228 n = monitor_puts(mon, buf); in monitor_vprintf()
233 int monitor_printf(Monitor *mon, const char *fmt, ...) in monitor_printf() argument
239 ret = monitor_vprintf(mon, fmt, ap); in monitor_printf()
244 void monitor_printc(Monitor *mon, int c) in monitor_printc() argument
246 monitor_printf(mon, "'"); in monitor_printc()
249 monitor_printf(mon, "\\'"); in monitor_printc()
252 monitor_printf(mon, "\\\\"); in monitor_printc()
255 monitor_printf(mon, "\\n"); in monitor_printc()
258 monitor_printf(mon, "\\r"); in monitor_printc()
262 monitor_printf(mon, "%c", c); in monitor_printc()
264 monitor_printf(mon, "\\x%02x", c); in monitor_printc()
268 monitor_printf(mon, "'"); in monitor_printc()
294 return -1; in error_vprintf_unless_qmp()
309 /* Limit guest-triggerable events to 1 per second */
339 Monitor *mon; in monitor_qapi_event_emit() local
343 QTAILQ_FOREACH(mon, &mon_list, entry) { in monitor_qapi_event_emit()
344 if (!monitor_is_qmp(mon)) { in monitor_qapi_event_emit()
348 qmp_mon = container_of(mon, MonitorQMP, common); in monitor_qapi_event_emit()
349 if (qmp_mon->commands != &qmp_cap_negotiation_commands) { in monitor_qapi_event_emit()
369 trace_monitor_protocol_event_queue(event, qdict, evconf->rate); in monitor_qapi_event_queue_no_reenter()
373 if (!evconf->rate) { in monitor_qapi_event_queue_no_reenter()
381 assert(!evstate || timer_pending(evstate->timer)); in monitor_qapi_event_queue_no_reenter()
385 * Timer is pending for (at least) evconf->rate ns after in monitor_qapi_event_queue_no_reenter()
389 qobject_unref(evstate->qdict); in monitor_qapi_event_queue_no_reenter()
390 evstate->qdict = qobject_ref(qdict); in monitor_qapi_event_queue_no_reenter()
393 * Last send was (at least) evconf->rate ns ago. in monitor_qapi_event_queue_no_reenter()
395 * monitor_qapi_event_handler() in evconf->rate ns. Any in monitor_qapi_event_queue_no_reenter()
403 evstate->event = event; in monitor_qapi_event_queue_no_reenter()
404 evstate->data = qobject_ref(data); in monitor_qapi_event_queue_no_reenter()
405 evstate->qdict = NULL; in monitor_qapi_event_queue_no_reenter()
406 evstate->timer = timer_new_ns(monitor_get_event_clock(), in monitor_qapi_event_queue_no_reenter()
410 timer_mod_ns(evstate->timer, now + evconf->rate); in monitor_qapi_event_queue_no_reenter()
420 * events in thread-local storage. in qapi_event_emit()
421 * TODO: remove this, make it re-enter safe. in qapi_event_emit()
437 ev->qdict = qobject_ref(qdict); in qapi_event_emit()
438 ev->event = event; in qapi_event_emit()
448 monitor_qapi_event_queue_no_reenter(ev->event, ev->qdict); in qapi_event_emit()
449 qobject_unref(ev->qdict); in qapi_event_emit()
457 * This function runs evconf->rate ns after sending a throttled
464 MonitorQAPIEventConf *evconf = &monitor_qapi_event_conf[evstate->event]; in monitor_qapi_event_handler()
466 trace_monitor_protocol_event_handler(evstate->event, evstate->qdict); in monitor_qapi_event_handler()
469 if (evstate->qdict) { in monitor_qapi_event_handler()
472 monitor_qapi_event_emit(evstate->event, evstate->qdict); in monitor_qapi_event_handler()
473 qobject_unref(evstate->qdict); in monitor_qapi_event_handler()
474 evstate->qdict = NULL; in monitor_qapi_event_handler()
475 timer_mod_ns(evstate->timer, now + evconf->rate); in monitor_qapi_event_handler()
478 qobject_unref(evstate->data); in monitor_qapi_event_handler()
479 timer_free(evstate->timer); in monitor_qapi_event_handler()
487 unsigned int hash = evstate->event * 255; in qapi_event_throttle_hash()
489 if (evstate->event == QAPI_EVENT_VSERPORT_CHANGE) { in qapi_event_throttle_hash()
490 hash += g_str_hash(qdict_get_str(evstate->data, "id")); in qapi_event_throttle_hash()
493 if (evstate->event == QAPI_EVENT_QUORUM_REPORT_BAD) { in qapi_event_throttle_hash()
494 hash += g_str_hash(qdict_get_str(evstate->data, "node-name")); in qapi_event_throttle_hash()
497 if (evstate->event == QAPI_EVENT_MEMORY_DEVICE_SIZE_CHANGE || in qapi_event_throttle_hash()
498 evstate->event == QAPI_EVENT_BLOCK_IO_ERROR) { in qapi_event_throttle_hash()
499 hash += g_str_hash(qdict_get_str(evstate->data, "qom-path")); in qapi_event_throttle_hash()
510 if (eva->event != evb->event) { in qapi_event_throttle_equal()
514 if (eva->event == QAPI_EVENT_VSERPORT_CHANGE) { in qapi_event_throttle_equal()
515 return !strcmp(qdict_get_str(eva->data, "id"), in qapi_event_throttle_equal()
516 qdict_get_str(evb->data, "id")); in qapi_event_throttle_equal()
519 if (eva->event == QAPI_EVENT_QUORUM_REPORT_BAD) { in qapi_event_throttle_equal()
520 return !strcmp(qdict_get_str(eva->data, "node-name"), in qapi_event_throttle_equal()
521 qdict_get_str(evb->data, "node-name")); in qapi_event_throttle_equal()
524 if (eva->event == QAPI_EVENT_MEMORY_DEVICE_SIZE_CHANGE || in qapi_event_throttle_equal()
525 eva->event == QAPI_EVENT_BLOCK_IO_ERROR) { in qapi_event_throttle_equal()
526 return !strcmp(qdict_get_str(eva->data, "qom-path"), in qapi_event_throttle_equal()
527 qdict_get_str(evb->data, "qom-path")); in qapi_event_throttle_equal()
533 int monitor_suspend(Monitor *mon) in monitor_suspend() argument
535 if (monitor_is_hmp_non_interactive(mon)) { in monitor_suspend()
536 return -ENOTTY; in monitor_suspend()
539 qatomic_inc(&mon->suspend_cnt); in monitor_suspend()
541 if (mon->use_io_thread) { in monitor_suspend()
549 trace_monitor_suspend(mon, 1); in monitor_suspend()
550 return 0; in monitor_suspend()
555 Monitor *mon = opaque; in monitor_accept_input() local
557 qemu_mutex_lock(&mon->mon_lock); in monitor_accept_input()
558 if (!monitor_is_qmp(mon) && mon->reset_seen) { in monitor_accept_input()
559 MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common); in monitor_accept_input()
560 assert(hmp_mon->rs); in monitor_accept_input()
561 readline_restart(hmp_mon->rs); in monitor_accept_input()
562 qemu_mutex_unlock(&mon->mon_lock); in monitor_accept_input()
563 readline_show_prompt(hmp_mon->rs); in monitor_accept_input()
565 qemu_mutex_unlock(&mon->mon_lock); in monitor_accept_input()
568 qemu_chr_fe_accept_input(&mon->chr); in monitor_accept_input()
571 void monitor_resume(Monitor *mon) in monitor_resume() argument
573 if (monitor_is_hmp_non_interactive(mon)) { in monitor_resume()
577 if (qatomic_dec_fetch(&mon->suspend_cnt) == 0) { in monitor_resume()
580 if (mon->use_io_thread) { in monitor_resume()
586 aio_bh_schedule_oneshot(ctx, monitor_accept_input, mon); in monitor_resume()
589 trace_monitor_suspend(mon, -1); in monitor_resume()
594 Monitor *mon = opaque; in monitor_can_read() local
596 return !qatomic_read(&mon->suspend_cnt); in monitor_can_read()
599 void monitor_list_append(Monitor *mon) in monitor_list_append() argument
608 QTAILQ_INSERT_HEAD(&mon_list, mon, entry); in monitor_list_append()
609 mon = NULL; in monitor_list_append()
613 if (mon) { in monitor_list_append()
614 monitor_data_destroy(mon); in monitor_list_append()
615 g_free(mon); in monitor_list_append()
624 void monitor_data_init(Monitor *mon, bool is_qmp, bool skip_flush, in monitor_data_init() argument
630 qemu_mutex_init(&mon->mon_lock); in monitor_data_init()
631 mon->is_qmp = is_qmp; in monitor_data_init()
632 mon->outbuf = g_string_new(NULL); in monitor_data_init()
633 mon->skip_flush = skip_flush; in monitor_data_init()
634 mon->use_io_thread = use_io_thread; in monitor_data_init()
637 void monitor_data_destroy(Monitor *mon) in monitor_data_destroy() argument
639 g_free(mon->mon_cpu_path); in monitor_data_destroy()
640 qemu_chr_fe_deinit(&mon->chr, false); in monitor_data_destroy()
641 if (monitor_is_qmp(mon)) { in monitor_data_destroy()
642 monitor_data_destroy_qmp(container_of(mon, MonitorQMP, common)); in monitor_data_destroy()
644 readline_free(container_of(mon, MonitorHMP, common)->rs); in monitor_data_destroy()
646 g_string_free(mon->outbuf, true); in monitor_data_destroy()
647 qemu_mutex_destroy(&mon->mon_lock); in monitor_data_destroy()
680 * monitor_data_destroy(), and chardev is not thread-safe yet in monitor_cleanup()
690 Monitor *mon = QTAILQ_FIRST(&mon_list); in monitor_cleanup() local
691 QTAILQ_REMOVE(&mon_list, mon, entry); in monitor_cleanup()
694 monitor_flush(mon); in monitor_cleanup()
695 monitor_data_destroy(mon); in monitor_cleanup()
697 g_free(mon); in monitor_cleanup()
733 chr = qemu_chr_find(opts->chardev); in monitor_init()
735 error_setg(errp, "chardev \"%s\" not found", opts->chardev); in monitor_init()
736 return -1; in monitor_init()
739 if (!opts->has_mode) { in monitor_init()
740 opts->mode = allow_hmp ? MONITOR_MODE_READLINE : MONITOR_MODE_CONTROL; in monitor_init()
743 switch (opts->mode) { in monitor_init()
745 monitor_init_qmp(chr, opts->pretty, errp); in monitor_init()
750 return -1; in monitor_init()
752 if (opts->pretty) { in monitor_init()
754 return -1; in monitor_init()
762 return *errp ? -1 : 0; in monitor_init()
775 return -1; in monitor_init_opts()
784 .name = "mon",