Instead of requiring a MonitorHMP and going through
monitor_hmp_vprintf(), use monitor_puts() on the base Monitor class.
This allows qemu_printf() to work with any monitor type that
isn't QMP, which will be needed to capture output via a bare
Monitor used as a buffer.

Add monitor_puts() since qemu_printf() is in libqemuutil.

Signed-off-by: Marc-André Lureau <[email protected]>
---
 stubs/meson.build    |  1 +
 stubs/monitor-puts.c |  9 +++++++++
 util/qemu-print.c    | 16 +++++-----------
 3 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/stubs/meson.build b/stubs/meson.build
index 29b6644b2549..52c733e32a91 100644
--- a/stubs/meson.build
+++ b/stubs/meson.build
@@ -9,6 +9,7 @@ stub_ss.add(files('is-daemonized.c'))
 stub_ss.add(files('monitor-core.c'))
 stub_ss.add(files('monitor-cur.c'))
 stub_ss.add(files('monitor-hmp.c'))
+stub_ss.add(files('monitor-puts.c'))
 stub_ss.add(files('qapi-event-emit.c'))
 stub_ss.add(files('replay-mode.c'))
 stub_ss.add(files('trace-control.c'))
diff --git a/stubs/monitor-puts.c b/stubs/monitor-puts.c
new file mode 100644
index 000000000000..c4cf4f632845
--- /dev/null
+++ b/stubs/monitor-puts.c
@@ -0,0 +1,9 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#include "qemu/osdep.h"
+#include "monitor/monitor.h"
+
+int monitor_puts(Monitor *mon, const char *str)
+{
+    abort();
+}
diff --git a/util/qemu-print.c b/util/qemu-print.c
index ab09dd8600ec..323789f265f9 100644
--- a/util/qemu-print.c
+++ b/util/qemu-print.c
@@ -19,25 +19,19 @@
 /*
  * Print like vprintf().
  * Print to current monitor if we have one, else to stdout.
- * (if the monitor is QMP, fail without printing anything)
+ * QMP monitors are rejected (return -1) since they don't accept
+ * free-form text output.
  */
 int qemu_vprintf(const char *fmt, va_list ap)
 {
     Monitor *cur_mon = monitor_cur();
 
-    /* for all monitors: QMP & HMP */
     if (cur_mon) {
-#ifdef CONFIG_HMP
-        /* don't use monitor_cur_hmp(), to avoid a second lookup */
-        MonitorHMP *hmp = (MonitorHMP *)
-            object_dynamic_cast(OBJECT(cur_mon), TYPE_MONITOR_HMP);
-        if (!hmp) {
+        if (object_dynamic_cast(OBJECT(cur_mon), TYPE_MONITOR_QMP)) {
             return -1;
         }
-        return monitor_hmp_vprintf(hmp, fmt, ap);
-#else
-        return -1;
-#endif
+        g_autofree char *buf = g_strdup_vprintf(fmt, ap);
+        return monitor_puts(cur_mon, buf);
     }
     return vprintf(fmt, ap);
 }

-- 
2.56.0.rc0.29.g47ce80527c56


Reply via email to