From: "Daniel P. Berrangé" <berrange@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Markus Armbruster" <armbru@redhat.com>,
"Dr. David Alan Gilbert" <dave@treblig.org>,
"Marc-André Lureau" <marcandre.lureau@redhat.com>,
"Paolo Bonzini" <pbonzini@redhat.com>,
"Daniel P. Berrangé" <berrange@redhat.com>,
"Peter Maydell" <peter.maydell@linaro.org>,
"Richard Henderson" <richard.henderson@linaro.org>
Subject: [PATCH 1/6] util: pull Location struct and APIs into location.{c,h}
Date: Thu, 3 Dec 2026 17:25:38 +0000 [thread overview]
Message-ID: <20261203172543.676434-2-berrange@redhat.com> (raw)
In-Reply-To: <20261203172543.676434-1-berrange@redhat.com>
The Location struct and APIs are currently alongside the
error report code, since the primary use of Location was
as error reporting.
We need to reference the location APIs from the new
message output APIs, but the latter is in turn going to
be referenced from the error report APIs which sets up
a circular loop. Splitting off the location APIs keeps
the code loop-free.
The new files are put under the existing MAINTAINERS
file entry for error reporting.
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
MAINTAINERS | 2 +
include/qemu/error-report.h | 17 +----
include/qemu/location.h | 24 +++++++
util/error-report.c | 118 ++------------------------------
util/location.c | 131 ++++++++++++++++++++++++++++++++++++
util/meson.build | 1 +
6 files changed, 163 insertions(+), 130 deletions(-)
create mode 100644 include/qemu/location.h
create mode 100644 util/location.c
diff --git a/MAINTAINERS b/MAINTAINERS
index ad9dfc43898..a565919d3de 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -3484,9 +3484,11 @@ M: Markus Armbruster <armbru@redhat.com>
S: Supported
F: include/qapi/error.h
F: include/qemu/error-report.h
+F: include/qemu/location.h
F: qapi/error.json
F: util/error.c
F: util/error-report.c
+F: util/location.c
F: scripts/coccinelle/err-bad-newline.cocci
F: scripts/coccinelle/error-use-after-free.cocci
F: scripts/coccinelle/error_propagate_null.cocci
diff --git a/include/qemu/error-report.h b/include/qemu/error-report.h
index dc423d36071..482bbcc8fd3 100644
--- a/include/qemu/error-report.h
+++ b/include/qemu/error-report.h
@@ -13,22 +13,7 @@
#ifndef QEMU_ERROR_REPORT_H
#define QEMU_ERROR_REPORT_H
-typedef struct Location {
- /* all members are private to qemu-error.c */
- enum { LOC_NONE, LOC_CMDLINE, LOC_FILE } kind;
- int num;
- const void *ptr;
- struct Location *prev;
-} Location;
-
-Location *loc_push_restore(Location *loc);
-Location *loc_push_none(Location *loc);
-Location *loc_pop(Location *loc);
-Location *loc_save(Location *loc);
-void loc_restore(Location *loc);
-void loc_set_none(void);
-void loc_set_cmdline(char **argv, int idx, int cnt);
-void loc_set_file(const char *fname, int lno);
+#include "qemu/location.h"
int error_vprintf(const char *fmt, va_list ap) G_GNUC_PRINTF(1, 0);
int error_printf(const char *fmt, ...) G_GNUC_PRINTF(1, 2);
diff --git a/include/qemu/location.h b/include/qemu/location.h
new file mode 100644
index 00000000000..c900ef9b979
--- /dev/null
+++ b/include/qemu/location.h
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#ifndef QEMU_LOCATION_H
+#define QEMU_LOCATION_H
+
+typedef struct Location {
+ /* all members are private to qemu-error.c */
+ enum { LOC_NONE, LOC_CMDLINE, LOC_FILE } kind;
+ int num;
+ const void *ptr;
+ struct Location *prev;
+} Location;
+
+Location *loc_push_restore(Location *loc);
+Location *loc_push_none(Location *loc);
+Location *loc_pop(Location *loc);
+Location *loc_save(Location *loc);
+void loc_restore(Location *loc);
+void loc_set_none(void);
+void loc_set_cmdline(char **argv, int idx, int cnt);
+void loc_set_file(const char *fname, int lno);
+char *loc_as_string(void);
+
+#endif /* QEMU_LOCATION_H */
diff --git a/util/error-report.c b/util/error-report.c
index 8932beebfb9..ebcd672d091 100644
--- a/util/error-report.c
+++ b/util/error-report.c
@@ -80,132 +80,22 @@ int error_printf(const char *fmt, ...)
return ret;
}
-static Location std_loc = {
- .kind = LOC_NONE
-};
-static Location *cur_loc = &std_loc;
-
-/*
- * Push location saved in LOC onto the location stack, return it.
- * The top of that stack is the current location.
- * Needs a matching loc_pop().
- */
-Location *loc_push_restore(Location *loc)
-{
- assert(!loc->prev);
- loc->prev = cur_loc;
- cur_loc = loc;
- return loc;
-}
-
-/*
- * Initialize *LOC to "nowhere", push it onto the location stack.
- * The top of that stack is the current location.
- * Needs a matching loc_pop().
- * Return LOC.
- */
-Location *loc_push_none(Location *loc)
-{
- loc->kind = LOC_NONE;
- loc->prev = NULL;
- return loc_push_restore(loc);
-}
-
-/*
- * Pop the location stack.
- * LOC must be the current location, i.e. the top of the stack.
- */
-Location *loc_pop(Location *loc)
-{
- assert(cur_loc == loc && loc->prev);
- cur_loc = loc->prev;
- loc->prev = NULL;
- return loc;
-}
-
-/*
- * Save the current location in LOC, return LOC.
- */
-Location *loc_save(Location *loc)
-{
- *loc = *cur_loc;
- loc->prev = NULL;
- return loc;
-}
-
-/*
- * Change the current location to the one saved in LOC.
- */
-void loc_restore(Location *loc)
-{
- Location *prev = cur_loc->prev;
- assert(!loc->prev);
- *cur_loc = *loc;
- cur_loc->prev = prev;
-}
-
-/*
- * Change the current location to "nowhere in particular".
- */
-void loc_set_none(void)
-{
- cur_loc->kind = LOC_NONE;
-}
-
-/*
- * Change the current location to argument ARGV[IDX..IDX+CNT-1].
- */
-void loc_set_cmdline(char **argv, int idx, int cnt)
-{
- cur_loc->kind = LOC_CMDLINE;
- cur_loc->num = cnt;
- cur_loc->ptr = argv + idx;
-}
-
-/*
- * Change the current location to file FNAME, line LNO.
- */
-void loc_set_file(const char *fname, int lno)
-{
- assert (fname || cur_loc->kind == LOC_FILE);
- cur_loc->kind = LOC_FILE;
- cur_loc->num = lno;
- if (fname) {
- cur_loc->ptr = fname;
- }
-}
-
/*
* Print current location to current HMP monitor if we have one, else
* to stderr.
*/
static void print_loc(MonitorHMP *hmp)
{
+ g_autofree const char *loc = loc_as_string();
const char *sep = "";
- int i;
- const char *const *argp;
if (!hmp && g_get_prgname()) {
fprintf(stderr, "%s:", g_get_prgname());
sep = " ";
}
- switch (cur_loc->kind) {
- case LOC_CMDLINE:
- argp = cur_loc->ptr;
- for (i = 0; i < cur_loc->num; i++) {
- error_printf_hmp(hmp, "%s%s", sep, argp[i]);
- sep = " ";
- }
- error_printf_hmp(hmp, ": ");
- break;
- case LOC_FILE:
- error_printf_hmp(hmp, "%s:", (const char *)cur_loc->ptr);
- if (cur_loc->num) {
- error_printf_hmp(hmp, "%d:", cur_loc->num);
- }
- error_printf_hmp(hmp, " ");
- break;
- default:
+ if (loc && *loc) {
+ error_printf_hmp(hmp, "%s%s: ", sep, loc);
+ } else {
error_printf_hmp(hmp, "%s", sep);
}
}
diff --git a/util/location.c b/util/location.c
new file mode 100644
index 00000000000..9693787039e
--- /dev/null
+++ b/util/location.c
@@ -0,0 +1,131 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#include "qemu/osdep.h"
+#include "qemu/location.h"
+
+
+static Location std_loc = {
+ .kind = LOC_NONE
+};
+static Location *cur_loc = &std_loc;
+
+/*
+ * Push location saved in LOC onto the location stack, return it.
+ * The top of that stack is the current location.
+ * Needs a matching loc_pop().
+ */
+Location *loc_push_restore(Location *loc)
+{
+ assert(!loc->prev);
+ loc->prev = cur_loc;
+ cur_loc = loc;
+ return loc;
+}
+
+/*
+ * Initialize *LOC to "nowhere", push it onto the location stack.
+ * The top of that stack is the current location.
+ * Needs a matching loc_pop().
+ * Return LOC.
+ */
+Location *loc_push_none(Location *loc)
+{
+ loc->kind = LOC_NONE;
+ loc->prev = NULL;
+ return loc_push_restore(loc);
+}
+
+/*
+ * Pop the location stack.
+ * LOC must be the current location, i.e. the top of the stack.
+ */
+Location *loc_pop(Location *loc)
+{
+ assert(cur_loc == loc && loc->prev);
+ cur_loc = loc->prev;
+ loc->prev = NULL;
+ return loc;
+}
+
+/*
+ * Save the current location in LOC, return LOC.
+ */
+Location *loc_save(Location *loc)
+{
+ *loc = *cur_loc;
+ loc->prev = NULL;
+ return loc;
+}
+
+/*
+ * Change the current location to the one saved in LOC.
+ */
+void loc_restore(Location *loc)
+{
+ Location *prev = cur_loc->prev;
+ assert(!loc->prev);
+ *cur_loc = *loc;
+ cur_loc->prev = prev;
+}
+
+/*
+ * Change the current location to "nowhere in particular".
+ */
+void loc_set_none(void)
+{
+ cur_loc->kind = LOC_NONE;
+}
+
+/*
+ * Change the current location to argument ARGV[IDX..IDX+CNT-1].
+ */
+void loc_set_cmdline(char **argv, int idx, int cnt)
+{
+ cur_loc->kind = LOC_CMDLINE;
+ cur_loc->num = cnt;
+ cur_loc->ptr = argv + idx;
+}
+
+/*
+ * Change the current location to file FNAME, line LNO.
+ */
+void loc_set_file(const char *fname, int lno)
+{
+ assert(fname || cur_loc->kind == LOC_FILE);
+ cur_loc->kind = LOC_FILE;
+ cur_loc->num = lno;
+ if (fname) {
+ cur_loc->ptr = fname;
+ }
+}
+
+/*
+ * Return the current location formatted as a string
+ */
+char *loc_as_string(void)
+{
+ GString *str = g_string_new("");
+ const char *sep = "";
+ int i;
+ const char *const *argp;
+
+ switch (cur_loc->kind) {
+ case LOC_CMDLINE:
+ argp = cur_loc->ptr;
+ for (i = 0; i < cur_loc->num; i++) {
+ g_string_append_printf(str, "%s%s", sep, argp[i]);
+ sep = " ";
+ }
+ break;
+ case LOC_FILE:
+ g_string_append_printf(str, "%s", (const char *)cur_loc->ptr);
+ if (cur_loc->num) {
+ g_string_append_printf(str, ":%d", cur_loc->num);
+ }
+ break;
+ default:
+ break;
+ }
+
+ return g_string_free(str, FALSE);
+}
diff --git a/util/meson.build b/util/meson.build
index fa174c07a57..0982481177b 100644
--- a/util/meson.build
+++ b/util/meson.build
@@ -39,6 +39,7 @@ util_ss.add(files('host-utils.c'))
util_ss.add(files('bitmap.c', 'bitops.c'))
util_ss.add(files('fifo8.c'))
util_ss.add(files('cacheflush.c'))
+util_ss.add(files('location.c'))
util_ss.add(files('error.c', 'error-report.c'))
util_ss.add(files('qemu-print.c'))
util_ss.add(files('id.c'))
--
2.56.1
next prev parent reply other threads:[~2026-12-03 17:26 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-03 17:25 [PATCH 0/6] util: configurable / flexible error/log message output format Daniel P. Berrangé
2026-12-03 17:25 ` Daniel P. Berrangé [this message]
2026-12-03 20:51 ` [PATCH 1/6] util: pull Location struct and APIs into location.{c,h} Eric Blake
2026-12-03 17:25 ` [PATCH 2/6] util: add helper to query if a location is set Daniel P. Berrangé
2026-12-03 20:51 ` Eric Blake
2026-12-03 17:25 ` [PATCH 3/6] util: introduce a configurable message format template Daniel P. Berrangé
2026-12-03 21:44 ` Eric Blake
2026-12-04 9:22 ` Daniel P. Berrangé
2026-12-04 0:32 ` Dr. David Alan Gilbert
2026-12-03 17:25 ` [PATCH 4/6] util: convert log code to support QMessageFormat Daniel P. Berrangé
2026-12-03 21:52 ` Eric Blake
2026-12-03 17:25 ` [PATCH 5/6] util: convert error report code to use QMessageFormat Daniel P. Berrangé
2026-12-03 21:55 ` Eric Blake
2026-12-03 17:25 ` [PATCH 6/6] util: add '-msg format=FORMATSTR' argument Daniel P. Berrangé
2026-12-03 22:02 ` Eric Blake
2026-12-04 9:23 ` Daniel P. Berrangé
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261203172543.676434-2-berrange@redhat.com \
--to=berrange@redhat.com \
--cc=armbru@redhat.com \
--cc=dave@treblig.org \
--cc=marcandre.lureau@redhat.com \
--cc=pbonzini@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=richard.henderson@linaro.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.