@samitouri / QOSamiQemu / commits / 7f01300c04

error-report: switch to use monitor_cur_hmp()

Thread through the current *hmp monitor, a non-NULL pointer is guaranteed to be a valid HMP monitor. Adjust doc comments. Rename _mon()->_hmp() for readability. No behavioral change. Reviewed-by: Daniel P. Berrangé <berrange@redhat.com> Reviewed-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com> Message-ID: <20260828-qemu-no-hmp-v5-35-9227de146347@redhat.com>

Marc-André Lureau committed Aug 28, 2026 at 16:04 UTC 7f01300c044e546f72248e30cb4112472f1c9828
1 file changed +29 -40
util/error-report.c
+29 -40
@@ -30,34 +30,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.
33 + * Print to the @hmp monitor, else to stderr.
34 */
35 static int G_GNUC_PRINTF(2, 0)
36 -error_vprintf_mon(Monitor *cur_mon, const char *fmt, va_list ap)
36 +error_vprintf_hmp(MonitorHMP *hmp, const char *fmt, va_list ap)
37 {
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);
38 + if (hmp) {
39 + return monitor_vprintf(MONITOR(hmp), fmt, ap);
40 }
47 - return ret;
41 +
42 + return vfprintf(stderr, fmt, ap);
43 }
44
45 /*
51 - * Print to the current human monitor if we have one, else to stderr.
46 + * Print to the @hmp monitor, else to stderr.
47 */
48 static int G_GNUC_PRINTF(2, 3)
54 -error_printf_mon(Monitor *cur_mon, const char *fmt, ...)
49 +error_printf_hmp(MonitorHMP *hmp, const char *fmt, ...)
50 {
51 va_list ap;
52 int ret;
53
54 va_start(ap, fmt);
60 - ret = error_vprintf_mon(cur_mon, fmt, ap);
55 + ret = error_vprintf_hmp(hmp, fmt, ap);
56 va_end(ap);
57 return ret;
58 }
@@ -67,7 +62,7 @@ error_printf_mon(Monitor *cur_mon, const char *fmt, ...)
62 */
63 int error_vprintf(const char *fmt, va_list ap)
64 {
70 - return error_vprintf_mon(monitor_cur(), fmt, ap);
65 + return error_vprintf_hmp(monitor_cur_hmp(), fmt, ap);
66 }
67
68 /*
@@ -79,7 +74,7 @@ int error_printf(const char *fmt, ...)
74 int ret;
75
76 va_start(ap, fmt);
82 - ret = error_vprintf_mon(monitor_cur(), fmt, ap);
77 + ret = error_vprintf_hmp(monitor_cur_hmp(), fmt, ap);
78 va_end(ap);
79 return ret;
80 }
@@ -183,13 +178,13 @@ void loc_set_file(const char *fname, int lno)
178 * Print current location to current HMP monitor if we have one, else
179 * to stderr.
180 */
186 -static void print_loc(Monitor *cur)
181 +static void print_loc(MonitorHMP *hmp)
182 {
183 const char *sep = "";
184 int i;
185 const char *const *argp;
186
192 - if (!cur && g_get_prgname()) {
187 + if (!hmp && g_get_prgname()) {
188 fprintf(stderr, "%s:", g_get_prgname());
189 sep = " ";
190 }
@@ -197,20 +192,20 @@ static void print_loc(Monitor *cur)
192 case LOC_CMDLINE:
193 argp = cur_loc->ptr;
194 for (i = 0; i < cur_loc->num; i++) {
200 - error_printf_mon(cur, "%s%s", sep, argp[i]);
195 + error_printf_hmp(hmp, "%s%s", sep, argp[i]);
196 sep = " ";
197 }
203 - error_printf_mon(cur, ": ");
198 + error_printf_hmp(hmp, ": ");
199 break;
200 case LOC_FILE:
206 - error_printf_mon(cur, "%s:", (const char *)cur_loc->ptr);
201 + error_printf_hmp(hmp, "%s:", (const char *)cur_loc->ptr);
202 if (cur_loc->num) {
208 - error_printf_mon(cur, "%d:", cur_loc->num);
203 + error_printf_hmp(hmp, "%d:", cur_loc->num);
204 }
210 - error_printf_mon(cur, " ");
205 + error_printf_hmp(hmp, " ");
206 break;
207 default:
213 - error_printf_mon(cur, "%s", sep);
208 + error_printf_hmp(hmp, "%s", sep);
209 }
210 }
211
@@ -231,47 +226,41 @@ char *real_time_iso8601(void)
226 G_GNUC_PRINTF(2, 0)
227 static void vreport(report_type type, const char *fmt, va_list ap)
228 {
234 - /*
235 - * When current monitor is QMP, messages must go to stderr
236 - * and have prefixes added, so we cast to HMP, leaving 'cur'
237 - * as NULL in QMP case
238 - */
239 - Monitor *cur = MONITOR(
240 - object_dynamic_cast(OBJECT(monitor_cur()), TYPE_MONITOR_HMP));
229 + MonitorHMP *hmp = monitor_cur_hmp();
230 gchar *timestr;
231
243 - if (!cur) {
232 + if (!hmp) {
233 qemu_flockfile(stderr);
234 }
235
247 - if (message_with_timestamp && !cur) {
236 + if (message_with_timestamp && !hmp) {
237 timestr = real_time_iso8601();
238 fprintf(stderr, "%s ", timestr);
239 g_free(timestr);
240 }
241
242 /* Only prepend guest name if -msg guest-name and -name guest=... are set */
254 - if (error_with_guestname && error_guest_name && !cur) {
243 + if (error_with_guestname && error_guest_name && !hmp) {
244 fprintf(stderr, "%s ", error_guest_name);
245 }
246
258 - print_loc(cur);
247 + print_loc(hmp);
248
249 switch (type) {
250 case REPORT_TYPE_ERROR:
251 break;
252 case REPORT_TYPE_WARNING:
264 - error_printf_mon(cur, "warning: ");
253 + error_printf_hmp(hmp, "warning: ");
254 break;
255 case REPORT_TYPE_INFO:
267 - error_printf_mon(cur, "info: ");
256 + error_printf_hmp(hmp, "info: ");
257 break;
258 }
259
271 - error_vprintf_mon(cur, fmt, ap);
272 - error_printf_mon(cur, "\n");
260 + error_vprintf_hmp(hmp, fmt, ap);
261 + error_printf_hmp(hmp, "\n");
262
274 - if (!cur) {
263 + if (!hmp) {
264 qemu_funlockfile(stderr);
265 }
266 }