From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1277E606A322 for ; Thu, 3 Dec 2026 17:26:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=irVzq+lR Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xZAZE-0002zr-8e; Thu, 03 Dec 2026 12:26:00 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xZAZC-0002zM-MZ for qemu-devel@nongnu.org; Thu, 03 Dec 2026 12:25:58 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xZAZA-0007dS-EY for qemu-devel@nongnu.org; Thu, 03 Dec 2026 12:25:58 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796318756; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=MRzTM5BvN23oth9AjShn1LS2DwcmckQpE6af4RatrY8=; b=irVzq+lRHF+0RBAw6lBb6BO9qsar9ViZVhNB5caM6HidtP8AmYci2+U7vgSBtmx1Ov9MO+ 96omS71MG8mW7vXdN6KnjUNBdYnFbg9lv/My3MsnmDeM7DyRdddPQb4OE5gPR2DS9qM5Mo Va8/vInJKiyYWpd87D1afaXmgG5sSOs= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-315-BZzGrBe4MtqdP-ig3-eRNA-1; Thu, 3 Dec 2026 17:25:50 +0000 X-MC-Unique: BZzGrBe4MtqdP-ig3-eRNA-1 X-Mimecast-MFC-AGG-ID: BZzGrBe4MtqdP-ig3-eRNA_1796318749 Received: from mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.95]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 4C0621889B68; Thu, 3 Dec 2026 17:25:49 +0000 (UTC) Received: from berrange.csb (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 6961AB9A; Thu, 3 Dec 2026 17:25:47 +0000 (UTC) From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Cc: Markus Armbruster , "Dr. David Alan Gilbert" , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Paolo Bonzini , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Peter Maydell , Richard Henderson Subject: [PATCH 1/6] util: pull Location struct and APIs into location.{c,h} Date: Thu, 3 Dec 2026 17:25:38 +0000 Message-ID: <20261203172543.676434-2-berrange@redhat.com> In-Reply-To: <20261203172543.676434-1-berrange@redhat.com> References: <20261203172543.676434-1-berrange@redhat.com> X-Archived-By: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.6 on 10.30.177.95 Received-SPF: pass client-ip=170.10.129.124; envelope-from=berrange@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.24, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 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é --- 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 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