1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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 "qemu/error-report.h"
33#include "qemu/option.h"
34#include "sysemu/qtest.h"
35#include "trace.h"
36
37
38
39
40
41
42typedef struct MonitorQAPIEventState {
43 QAPIEvent event;
44 QDict *data;
45 QEMUTimer *timer;
46 QDict *qdict;
47} MonitorQAPIEventState;
48
49typedef struct {
50 int64_t rate;
51} MonitorQAPIEventConf;
52
53
54IOThread *mon_iothread;
55
56
57Coroutine *qmp_dispatcher_co;
58
59
60bool qmp_dispatcher_co_shutdown;
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81bool qmp_dispatcher_co_busy;
82
83
84
85
86
87QemuMutex monitor_lock;
88static GHashTable *monitor_qapi_event_state;
89static GHashTable *coroutine_mon;
90
91MonitorList mon_list;
92int mon_refcount;
93static bool monitor_destroyed;
94
95Monitor *monitor_cur(void)
96{
97 Monitor *mon;
98
99 qemu_mutex_lock(&monitor_lock);
100 mon = g_hash_table_lookup(coroutine_mon, qemu_coroutine_self());
101 qemu_mutex_unlock(&monitor_lock);
102
103 return mon;
104}
105
106
107
108
109
110
111
112
113Monitor *monitor_set_cur(Coroutine *co, Monitor *mon)
114{
115 Monitor *old_monitor = monitor_cur();
116
117 qemu_mutex_lock(&monitor_lock);
118 if (mon) {
119 g_hash_table_replace(coroutine_mon, co, mon);
120 } else {
121 g_hash_table_remove(coroutine_mon, co);
122 }
123 qemu_mutex_unlock(&monitor_lock);
124
125 return old_monitor;
126}
127
128
129
130
131bool monitor_cur_is_qmp(void)
132{
133 Monitor *cur_mon = monitor_cur();
134
135 return cur_mon && monitor_is_qmp(cur_mon);
136}
137
138
139
140
141
142
143static inline bool monitor_uses_readline(const MonitorHMP *mon)
144{
145 return mon->use_readline;
146}
147
148static inline bool monitor_is_hmp_non_interactive(const Monitor *mon)
149{
150 if (monitor_is_qmp(mon)) {
151 return false;
152 }
153
154 return !monitor_uses_readline(container_of(mon, MonitorHMP, common));
155}
156
157static void monitor_flush_locked(Monitor *mon);
158
159static gboolean monitor_unblocked(void *do_not_use, GIOCondition cond,
160 void *opaque)
161{
162 Monitor *mon = opaque;
163
164 qemu_mutex_lock(&mon->mon_lock);
165 mon->out_watch = 0;
166 monitor_flush_locked(mon);
167 qemu_mutex_unlock(&mon->mon_lock);
168 return FALSE;
169}
170
171
172static void monitor_flush_locked(Monitor *mon)
173{
174 int rc;
175 size_t len;
176 const char *buf;
177
178 if (mon->skip_flush) {
179 return;
180 }
181
182 buf = mon->outbuf->str;
183 len = mon->outbuf->len;
184
185 if (len && !mon->mux_out) {
186 rc = qemu_chr_fe_write(&mon->chr, (const uint8_t *) buf, len);
187 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
188
189 g_string_truncate(mon->outbuf, 0);
190 return;
191 }
192 if (rc > 0) {
193
194 g_string_erase(mon->outbuf, 0, rc);
195 }
196 if (mon->out_watch == 0) {
197 mon->out_watch =
198 qemu_chr_fe_add_watch(&mon->chr, G_IO_OUT | G_IO_HUP,
199 monitor_unblocked, mon);
200 }
201 }
202}
203
204void monitor_flush(Monitor *mon)
205{
206 qemu_mutex_lock(&mon->mon_lock);
207 monitor_flush_locked(mon);
208 qemu_mutex_unlock(&mon->mon_lock);
209}
210
211
212int monitor_puts(Monitor *mon, const char *str)
213{
214 int i;
215 char c;
216
217 qemu_mutex_lock(&mon->mon_lock);
218 for (i = 0; str[i]; i++) {
219 c = str[i];
220 if (c == '\n') {
221 g_string_append_c(mon->outbuf, '\r');
222 }
223 g_string_append_c(mon->outbuf, c);
224 if (c == '\n') {
225 monitor_flush_locked(mon);
226 }
227 }
228 qemu_mutex_unlock(&mon->mon_lock);
229
230 return i;
231}
232
233int monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
234{
235 char *buf;
236 int n;
237
238 if (!mon) {
239 return -1;
240 }
241
242 if (monitor_is_qmp(mon)) {
243 return -1;
244 }
245
246 buf = g_strdup_vprintf(fmt, ap);
247 n = monitor_puts(mon, buf);
248 g_free(buf);
249 return n;
250}
251
252int monitor_printf(Monitor *mon, const char *fmt, ...)
253{
254 int ret;
255
256 va_list ap;
257 va_start(ap, fmt);
258 ret = monitor_vprintf(mon, fmt, ap);
259 va_end(ap);
260 return ret;
261}
262
263void monitor_printc(Monitor *mon, int c)
264{
265 monitor_printf(mon, "'");
266 switch(c) {
267 case '\'':
268 monitor_printf(mon, "\\'");
269 break;
270 case '\\':
271 monitor_printf(mon, "\\\\");
272 break;
273 case '\n':
274 monitor_printf(mon, "\\n");
275 break;
276 case '\r':
277 monitor_printf(mon, "\\r");
278 break;
279 default:
280 if (c >= 32 && c <= 126) {
281 monitor_printf(mon, "%c", c);
282 } else {
283 monitor_printf(mon, "\\x%02x", c);
284 }
285 break;
286 }
287 monitor_printf(mon, "'");
288}
289
290
291
292
293int error_vprintf(const char *fmt, va_list ap)
294{
295 Monitor *cur_mon = monitor_cur();
296
297 if (cur_mon && !monitor_cur_is_qmp()) {
298 return monitor_vprintf(cur_mon, fmt, ap);
299 }
300 return vfprintf(stderr, fmt, ap);
301}
302
303int error_vprintf_unless_qmp(const char *fmt, va_list ap)
304{
305 Monitor *cur_mon = monitor_cur();
306
307 if (!cur_mon) {
308 return vfprintf(stderr, fmt, ap);
309 }
310 if (!monitor_cur_is_qmp()) {
311 return monitor_vprintf(cur_mon, fmt, ap);
312 }
313 return -1;
314}
315
316int error_printf_unless_qmp(const char *fmt, ...)
317{
318 va_list ap;
319 int ret;
320
321 va_start(ap, fmt);
322 ret = error_vprintf_unless_qmp(fmt, ap);
323 va_end(ap);
324 return ret;
325}
326
327static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = {
328
329 [QAPI_EVENT_RTC_CHANGE] = { 1000 * SCALE_MS },
330 [QAPI_EVENT_WATCHDOG] = { 1000 * SCALE_MS },
331 [QAPI_EVENT_BALLOON_CHANGE] = { 1000 * SCALE_MS },
332 [QAPI_EVENT_QUORUM_REPORT_BAD] = { 1000 * SCALE_MS },
333 [QAPI_EVENT_QUORUM_FAILURE] = { 1000 * SCALE_MS },
334 [QAPI_EVENT_VSERPORT_CHANGE] = { 1000 * SCALE_MS },
335 [QAPI_EVENT_MEMORY_DEVICE_SIZE_CHANGE] = { 1000 * SCALE_MS },
336};
337
338
339
340
341
342
343
344static inline QEMUClockType monitor_get_event_clock(void)
345{
346 return qtest_enabled() ? QEMU_CLOCK_VIRTUAL : QEMU_CLOCK_REALTIME;
347}
348
349
350
351
352
353
354static void monitor_qapi_event_emit(QAPIEvent event, QDict *qdict)
355{
356 Monitor *mon;
357 MonitorQMP *qmp_mon;
358
359 trace_monitor_protocol_event_emit(event, qdict);
360 QTAILQ_FOREACH(mon, &mon_list, entry) {
361 if (!monitor_is_qmp(mon)) {
362 continue;
363 }
364
365 qmp_mon = container_of(mon, MonitorQMP, common);
366 if (qmp_mon->commands != &qmp_cap_negotiation_commands) {
367 qmp_send_response(qmp_mon, qdict);
368 }
369 }
370}
371
372static void monitor_qapi_event_handler(void *opaque);
373
374
375
376
377
378static void
379monitor_qapi_event_queue_no_reenter(QAPIEvent event, QDict *qdict)
380{
381 MonitorQAPIEventConf *evconf;
382 MonitorQAPIEventState *evstate;
383
384 assert(event < QAPI_EVENT__MAX);
385 evconf = &monitor_qapi_event_conf[event];
386 trace_monitor_protocol_event_queue(event, qdict, evconf->rate);
387
388 QEMU_LOCK_GUARD(&monitor_lock);
389
390 if (!evconf->rate) {
391
392 monitor_qapi_event_emit(event, qdict);
393 } else {
394 QDict *data = qobject_to(QDict, qdict_get(qdict, "data"));
395 MonitorQAPIEventState key = { .event = event, .data = data };
396
397 evstate = g_hash_table_lookup(monitor_qapi_event_state, &key);
398 assert(!evstate || timer_pending(evstate->timer));
399
400 if (evstate) {
401
402
403
404
405
406 qobject_unref(evstate->qdict);
407 evstate->qdict = qobject_ref(qdict);
408 } else {
409
410
411
412
413
414
415 int64_t now = qemu_clock_get_ns(monitor_get_event_clock());
416
417 monitor_qapi_event_emit(event, qdict);
418
419 evstate = g_new(MonitorQAPIEventState, 1);
420 evstate->event = event;
421 evstate->data = qobject_ref(data);
422 evstate->qdict = NULL;
423 evstate->timer = timer_new_ns(monitor_get_event_clock(),
424 monitor_qapi_event_handler,
425 evstate);
426 g_hash_table_add(monitor_qapi_event_state, evstate);
427 timer_mod_ns(evstate->timer, now + evconf->rate);
428 }
429 }
430}
431
432void qapi_event_emit(QAPIEvent event, QDict *qdict)
433{
434
435
436
437
438
439
440 typedef struct MonitorQapiEvent {
441 QAPIEvent event;
442 QDict *qdict;
443 QSIMPLEQ_ENTRY(MonitorQapiEvent) entry;
444 } MonitorQapiEvent;
445 static __thread QSIMPLEQ_HEAD(, MonitorQapiEvent) event_queue;
446 static __thread bool reentered;
447 MonitorQapiEvent *ev;
448
449 if (!reentered) {
450 QSIMPLEQ_INIT(&event_queue);
451 }
452
453 ev = g_new(MonitorQapiEvent, 1);
454 ev->qdict = qobject_ref(qdict);
455 ev->event = event;
456 QSIMPLEQ_INSERT_TAIL(&event_queue, ev, entry);
457 if (reentered) {
458 return;
459 }
460
461 reentered = true;
462
463 while ((ev = QSIMPLEQ_FIRST(&event_queue)) != NULL) {
464 QSIMPLEQ_REMOVE_HEAD(&event_queue, entry);
465 monitor_qapi_event_queue_no_reenter(ev->event, ev->qdict);
466 qobject_unref(ev->qdict);
467 g_free(ev);
468 }
469
470 reentered = false;
471}
472
473
474
475
476
477
478static void monitor_qapi_event_handler(void *opaque)
479{
480 MonitorQAPIEventState *evstate = opaque;
481 MonitorQAPIEventConf *evconf = &monitor_qapi_event_conf[evstate->event];
482
483 trace_monitor_protocol_event_handler(evstate->event, evstate->qdict);
484 QEMU_LOCK_GUARD(&monitor_lock);
485
486 if (evstate->qdict) {
487 int64_t now = qemu_clock_get_ns(monitor_get_event_clock());
488
489 monitor_qapi_event_emit(evstate->event, evstate->qdict);
490 qobject_unref(evstate->qdict);
491 evstate->qdict = NULL;
492 timer_mod_ns(evstate->timer, now + evconf->rate);
493 } else {
494 g_hash_table_remove(monitor_qapi_event_state, evstate);
495 qobject_unref(evstate->data);
496 timer_free(evstate->timer);
497 g_free(evstate);
498 }
499}
500
501static unsigned int qapi_event_throttle_hash(const void *key)
502{
503 const MonitorQAPIEventState *evstate = key;
504 unsigned int hash = evstate->event * 255;
505
506 if (evstate->event == QAPI_EVENT_VSERPORT_CHANGE) {
507 hash += g_str_hash(qdict_get_str(evstate->data, "id"));
508 }
509
510 if (evstate->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
511 hash += g_str_hash(qdict_get_str(evstate->data, "node-name"));
512 }
513
514 if (evstate->event == QAPI_EVENT_MEMORY_DEVICE_SIZE_CHANGE) {
515 hash += g_str_hash(qdict_get_str(evstate->data, "qom-path"));
516 }
517
518 return hash;
519}
520
521static gboolean qapi_event_throttle_equal(const void *a, const void *b)
522{
523 const MonitorQAPIEventState *eva = a;
524 const MonitorQAPIEventState *evb = b;
525
526 if (eva->event != evb->event) {
527 return FALSE;
528 }
529
530 if (eva->event == QAPI_EVENT_VSERPORT_CHANGE) {
531 return !strcmp(qdict_get_str(eva->data, "id"),
532 qdict_get_str(evb->data, "id"));
533 }
534
535 if (eva->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
536 return !strcmp(qdict_get_str(eva->data, "node-name"),
537 qdict_get_str(evb->data, "node-name"));
538 }
539
540 if (eva->event == QAPI_EVENT_MEMORY_DEVICE_SIZE_CHANGE) {
541 return !strcmp(qdict_get_str(eva->data, "qom-path"),
542 qdict_get_str(evb->data, "qom-path"));
543 }
544
545 return TRUE;
546}
547
548int monitor_suspend(Monitor *mon)
549{
550 if (monitor_is_hmp_non_interactive(mon)) {
551 return -ENOTTY;
552 }
553
554 qatomic_inc(&mon->suspend_cnt);
555
556 if (mon->use_io_thread) {
557
558
559
560
561 aio_notify(iothread_get_aio_context(mon_iothread));
562 }
563
564 trace_monitor_suspend(mon, 1);
565 return 0;
566}
567
568static void monitor_accept_input(void *opaque)
569{
570 Monitor *mon = opaque;
571
572 qemu_chr_fe_accept_input(&mon->chr);
573}
574
575void monitor_resume(Monitor *mon)
576{
577 if (monitor_is_hmp_non_interactive(mon)) {
578 return;
579 }
580
581 if (qatomic_dec_fetch(&mon->suspend_cnt) == 0) {
582 AioContext *ctx;
583
584 if (mon->use_io_thread) {
585 ctx = iothread_get_aio_context(mon_iothread);
586 } else {
587 ctx = qemu_get_aio_context();
588 }
589
590 if (!monitor_is_qmp(mon)) {
591 MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common);
592 assert(hmp_mon->rs);
593 readline_show_prompt(hmp_mon->rs);
594 }
595
596 aio_bh_schedule_oneshot(ctx, monitor_accept_input, mon);
597 }
598
599 trace_monitor_suspend(mon, -1);
600}
601
602int monitor_can_read(void *opaque)
603{
604 Monitor *mon = opaque;
605
606 return !qatomic_mb_read(&mon->suspend_cnt);
607}
608
609void monitor_list_append(Monitor *mon)
610{
611 qemu_mutex_lock(&monitor_lock);
612
613
614
615
616
617 if (!monitor_destroyed) {
618 QTAILQ_INSERT_HEAD(&mon_list, mon, entry);
619 mon = NULL;
620 }
621 qemu_mutex_unlock(&monitor_lock);
622
623 if (mon) {
624 monitor_data_destroy(mon);
625 g_free(mon);
626 }
627}
628
629static void monitor_iothread_init(void)
630{
631 mon_iothread = iothread_create("mon_iothread", &error_abort);
632}
633
634void monitor_data_init(Monitor *mon, bool is_qmp, bool skip_flush,
635 bool use_io_thread)
636{
637 if (use_io_thread && !mon_iothread) {
638 monitor_iothread_init();
639 }
640 qemu_mutex_init(&mon->mon_lock);
641 mon->is_qmp = is_qmp;
642 mon->outbuf = g_string_new(NULL);
643 mon->skip_flush = skip_flush;
644 mon->use_io_thread = use_io_thread;
645}
646
647void monitor_data_destroy(Monitor *mon)
648{
649 g_free(mon->mon_cpu_path);
650 qemu_chr_fe_deinit(&mon->chr, false);
651 if (monitor_is_qmp(mon)) {
652 monitor_data_destroy_qmp(container_of(mon, MonitorQMP, common));
653 } else {
654 readline_free(container_of(mon, MonitorHMP, common)->rs);
655 }
656 g_string_free(mon->outbuf, true);
657 qemu_mutex_destroy(&mon->mon_lock);
658}
659
660void monitor_cleanup(void)
661{
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677 qmp_dispatcher_co_shutdown = true;
678 if (!qatomic_xchg(&qmp_dispatcher_co_busy, true)) {
679 aio_co_wake(qmp_dispatcher_co);
680 }
681
682 AIO_WAIT_WHILE(qemu_get_aio_context(),
683 (aio_poll(iohandler_get_aio_context(), false),
684 qatomic_mb_read(&qmp_dispatcher_co_busy)));
685
686
687
688
689
690
691
692 if (mon_iothread) {
693 iothread_stop(mon_iothread);
694 }
695
696
697 qemu_mutex_lock(&monitor_lock);
698 monitor_destroyed = true;
699 while (!QTAILQ_EMPTY(&mon_list)) {
700 Monitor *mon = QTAILQ_FIRST(&mon_list);
701 QTAILQ_REMOVE(&mon_list, mon, entry);
702
703 qemu_mutex_unlock(&monitor_lock);
704 monitor_flush(mon);
705 monitor_data_destroy(mon);
706 qemu_mutex_lock(&monitor_lock);
707 g_free(mon);
708 }
709 qemu_mutex_unlock(&monitor_lock);
710
711 if (mon_iothread) {
712 iothread_destroy(mon_iothread);
713 mon_iothread = NULL;
714 }
715}
716
717static void monitor_qapi_event_init(void)
718{
719 monitor_qapi_event_state = g_hash_table_new(qapi_event_throttle_hash,
720 qapi_event_throttle_equal);
721}
722
723void monitor_init_globals(void)
724{
725 monitor_qapi_event_init();
726 qemu_mutex_init(&monitor_lock);
727 coroutine_mon = g_hash_table_new(NULL, NULL);
728
729
730
731
732
733
734 qmp_dispatcher_co = qemu_coroutine_create(monitor_qmp_dispatcher_co, NULL);
735 qatomic_mb_set(&qmp_dispatcher_co_busy, true);
736 aio_co_schedule(iohandler_get_aio_context(), qmp_dispatcher_co);
737}
738
739int monitor_init(MonitorOptions *opts, bool allow_hmp, Error **errp)
740{
741 ERRP_GUARD();
742 Chardev *chr;
743
744 chr = qemu_chr_find(opts->chardev);
745 if (chr == NULL) {
746 error_setg(errp, "chardev \"%s\" not found", opts->chardev);
747 return -1;
748 }
749
750 if (!opts->has_mode) {
751 opts->mode = allow_hmp ? MONITOR_MODE_READLINE : MONITOR_MODE_CONTROL;
752 }
753
754 switch (opts->mode) {
755 case MONITOR_MODE_CONTROL:
756 monitor_init_qmp(chr, opts->pretty, errp);
757 break;
758 case MONITOR_MODE_READLINE:
759 if (!allow_hmp) {
760 error_setg(errp, "Only QMP is supported");
761 return -1;
762 }
763 if (opts->pretty) {
764 error_setg(errp, "'pretty' is not compatible with HMP monitors");
765 return -1;
766 }
767 monitor_init_hmp(chr, true, errp);
768 break;
769 default:
770 g_assert_not_reached();
771 }
772
773 return *errp ? -1 : 0;
774}
775
776int monitor_init_opts(QemuOpts *opts, Error **errp)
777{
778 Visitor *v;
779 MonitorOptions *options;
780 int ret;
781
782 v = opts_visitor_new(opts);
783 visit_type_MonitorOptions(v, NULL, &options, errp);
784 visit_free(v);
785 if (!options) {
786 return -1;
787 }
788
789 ret = monitor_init(options, true, errp);
790 qapi_free_MonitorOptions(options);
791 return ret;
792}
793
794QemuOptsList qemu_mon_opts = {
795 .name = "mon",
796 .implied_opt_name = "chardev",
797 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
798 .desc = {
799 {
800 .name = "mode",
801 .type = QEMU_OPT_STRING,
802 },{
803 .name = "chardev",
804 .type = QEMU_OPT_STRING,
805 },{
806 .name = "pretty",
807 .type = QEMU_OPT_BOOL,
808 },
809 { }
810 },
811};
812