@samitouri / QOSamiQemu / commits / a582a5784e

monitor: move error_vprintf back to error-report.c

The current unit tests rely on monitor.o not being linked, such that the monitor stubs get linked instead. Since error_vprintf is in monitor.o this allows a stub error_vprintf impl to be used that calls g_test_message. This takes a different approach, with error_vprintf moving back to error-report.c such that it is always linked into the tests. The monitor_vprintf() stub is then changed to use g_test_message if QTEST_SILENT_ERRORS is set, otherwise it will return -1 and trigger error_vprintf to call vfprintf. The end result is functionally equivalent for the purposes of the unit tests. Reviewed-by: Richard Henderson <richard.henderson@linaro.org> Reviewed-by: Eric Blake <eblake@redhat.com> Reviewed-by: Markus Armbruster <armbru@redhat.com> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>

Daniel P. Berrangé committed Sep 24, 2025 at 16:25 UTC a582a5784e5fa8a52203b79488f5027cd4bf2c80
5 files changed +31 -38
monitor/monitor.c
-18
@@ -268,24 +268,6 @@ void monitor_printc(Monitor *mon, int c)
268 monitor_printf(mon, "'");
269 }
270
271 -/*
272 - * Print to the current human monitor if we have one, else to stderr.
273 - */
274 -int error_vprintf(const char *fmt, va_list ap)
275 -{
276 - Monitor *cur_mon = monitor_cur();
277 - /*
278 - * This will return -1 if 'cur_mon' is NULL, or is QMP.
279 - * IOW this will only print if in HMP, otherwise we
280 - * fallback to stderr for QMP / no-monitor scenarios.
281 - */
282 - int ret = monitor_vprintf(cur_mon, fmt, ap);
283 - if (ret == -1) {
284 - ret = vfprintf(stderr, fmt, ap);
285 - }
286 - return ret;
287 -}
288 -
271 static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = {
272 /* Limit guest-triggerable events to 1 per second */
273 [QAPI_EVENT_RTC_CHANGE] = { 1000 * SCALE_MS },
stubs/error-printf.c deleted
-18
@@ -1,18 +0,0 @@
1 -#include "qemu/osdep.h"
2 -#include "qemu/error-report.h"
3 -#include "monitor/monitor.h"
4 -
5 -int error_vprintf(const char *fmt, va_list ap)
6 -{
7 - int ret;
8 -
9 - if (g_test_initialized() && !g_test_subprocess() &&
10 - getenv("QTEST_SILENT_ERRORS")) {
11 - char *msg = g_strdup_vprintf(fmt, ap);
12 - g_test_message("%s", msg);
13 - ret = strlen(msg);
14 - g_free(msg);
15 - return ret;
16 - }
17 - return vfprintf(stderr, fmt, ap);
18 -}
stubs/meson.build
-1
@@ -3,7 +3,6 @@
3 # below, so that it is clear who needs the stubbed functionality.
4
5 stub_ss.add(files('cpu-get-clock.c'))
6 -stub_ss.add(files('error-printf.c'))
6 stub_ss.add(files('fdset.c'))
7 stub_ss.add(files('iothread-lock.c'))
8 stub_ss.add(files('is-daemonized.c'))
stubs/monitor-core.c
+13 -1
@@ -18,5 +18,17 @@ void qapi_event_emit(QAPIEvent event, QDict *qdict)
18
19 int monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
20 {
21 - abort();
21 + /*
22 + * Pretend 'g_test_message' is our monitor console to
23 + * stop the caller sending messages to stderr
24 + */
25 + if (g_test_initialized() && !g_test_subprocess() &&
26 + getenv("QTEST_SILENT_ERRORS")) {
27 + char *msg = g_strdup_vprintf(fmt, ap);
28 + g_test_message("%s", msg);
29 + size_t ret = strlen(msg);
30 + g_free(msg);
31 + return ret;
32 + }
33 + return -1;
34 }
util/error-report.c
+18
@@ -29,6 +29,24 @@ bool message_with_timestamp;
29 bool error_with_guestname;
30 const char *error_guest_name;
31
32 +/*
33 + * Print to the current human monitor if we have one, else to stderr.
34 + */
35 +int error_vprintf(const char *fmt, va_list ap)
36 +{
37 + Monitor *cur_mon = monitor_cur();
38 + /*
39 + * This will return -1 if 'cur_mon' is NULL, or is QMP.
40 + * IOW this will only print if in HMP, otherwise we
41 + * fallback to stderr for QMP / no-monitor scenarios.
42 + */
43 + int ret = monitor_vprintf(cur_mon, fmt, ap);
44 + if (ret == -1) {
45 + ret = vfprintf(stderr, fmt, ap);
46 + }
47 + return ret;
48 +}
49 +
50 int error_printf(const char *fmt, ...)
51 {
52 va_list ap;