From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id mem8AglN1mqbODMAWB0awg (envelope-from ) for ; Mon, 19 Oct 2026 13:02:01 -0400 Received: by simark.ca (Postfix, from userid 112) id 0532C1E04E; Mon, 19 Oct 2026 13:02:01 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-2.3 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id BA6BD1E04E for ; Mon, 19 Oct 2026 13:01:59 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 532494BA7983 for ; Mon, 19 Oct 2026 17:01:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 532494BA7983 Received: from simark.ca (simark.ca [158.69.221.121]) by sourceware.org (Postfix) with ESMTPS id 7102E4BA2E2F; Mon, 19 Oct 2026 16:59:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7102E4BA2E2F Authentication-Results: sourceware.org; dmarc=fail (p=none dis=none) header.from=efficios.com Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=efficios.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 7102E4BA2E2F Authentication-Results: sourceware.org; arc=none smtp.remote-ip=158.69.221.121 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1792429173; cv=none; b=wYaByyAQJGZ/V2DR+ekfWhU04TsasW6urUokTYeQKwDW95QAWL24K2xvk2mkOrwhN8Td8olhV/z4uTB+C+tmjEBuTBxrFxU6ABUFPpH+eW6W+7SP6RO8XJbiiCIgHhdl4sUgUnsjSiX0EKRqfsL7R8BlU3yd/z7Vtnqop/6r88U= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1792429173; c=relaxed/simple; bh=b9OXAU+AQg9XUb96kKkUVYcqzf8QUL5vJLU47JqO3T8=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=Bc0xk+M0D1AsbTIMgYuxoyxyDomOlF40PAlRcwGjKxKEqFT4msXyVvu7jvmdmiQP+60oOGRukqHGBtO2wNk76FpfnR/MQp1m3vxydZA7bbt7oPYwF/0JUJGRiimQ2GO7nH9zZ1VyD9f1Lr/C4wVpEEFatn384NPeposEGCtxdfs= ARC-Authentication-Results: i=1; sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7102E4BA2E2F Received: by simark.ca (Postfix) id 9696D1E050; Mon, 19 Oct 2026 12:59:31 -0400 (EDT) From: Simon Marchi To: gcc-patches@sourceware.org, gdb-patches@sourceware.org Cc: Simon Marchi Subject: [PATCH 2/6] gdbsupport/selftest: print expression when selftest fails and re-format Date: Mon, 19 Oct 2026 12:58:18 -0400 Message-ID: <20261019165929.792697-3-simon.marchi@efficios.com> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261019165929.792697-1-simon.marchi@efficios.com> References: <20261019165929.792697-1-simon.marchi@efficios.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org This is a small step towards printing rich error messages when selftests fail. Add a selftests::failed_selftest function, called by SELF_CHECK when the check fails. It formats the error message, which now includes the text of the failed expression (passed as a string from the SELF_CHECK macro), and throws an error with the new error code SELF_CHECK_FAILED_ERROR. Change the output to a single line starting with "FAIL:", followed by the location and the expression. The SELF_CHECK_FAILED_ERROR error code allows the selftest runner to tell a failed SELF_CHECK apart from other errors thrown during a test, and report them differently. This changes the output when there are failed selftests from: $ ./gdb -nx -q --data-directory=data-directory -ex "maint selftest string_" -batch Running selftest string_appendf. Running selftest string_printf. Self test failed: self-test failed at /home/smarchi/src/binutils-gdb/gdb/unittests/common-utils-selftests.c:35 Running selftest string_vappendf. Running selftest string_vprintf. Self test failed: self-test failed at /home/smarchi/src/binutils-gdb/gdb/unittests/common-utils-selftests.c:35 Failures: string_printf string_vprintf Ran 4 unit tests, 2 failed to: $ ./gdb -nx -q --data-directory=data-directory -ex "maint selftest string_" -batch Running selftest string_appendf. Running selftest string_printf. FAIL: /home/smarchi/src/binutils-gdb/gdb/unittests/common-utils-selftests.c:35: func ("%s", "test") == "test boo" Running selftest string_vappendf. Running selftest string_vprintf. FAIL: /home/smarchi/src/binutils-gdb/gdb/unittests/common-utils-selftests.c:35: func ("%s", "test") == "test boo" Failures: string_printf string_vprintf Ran 4 unit tests, 2 failed Other errors thrown during a selftest, which used to be printed as: Self test failed: oh no are now printed as: FAIL: unexpected exception: oh no The output looks similar with gdbserver (the formatting code is shared, so it's expected). I wish we could add some colors, but since this code is in gdbsupport, it's not straightforward. Maybe we can add that later. Change-Id: Ice0c930add2dc0d2395806cc17f9d2dc247db419 --- gdbsupport/common-exceptions.h | 3 +++ gdbsupport/selftest.cc | 15 ++++++++++++++- gdbsupport/selftest.h | 17 +++++++++++++---- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/gdbsupport/common-exceptions.h b/gdbsupport/common-exceptions.h index 058260b5f270..1b3de6a2f955 100644 --- a/gdbsupport/common-exceptions.h +++ b/gdbsupport/common-exceptions.h @@ -112,6 +112,9 @@ enum errors { "_ERROR" is appended to the name. */ MAX_COMPLETIONS_REACHED_ERROR, + /* A SELF_CHECK failed during a selftest. */ + SELF_CHECK_FAILED_ERROR, + /* Add more errors here. */ NR_ERRORS }; diff --git a/gdbsupport/selftest.cc b/gdbsupport/selftest.cc index 611bb990a0e2..85eee59e3c4b 100644 --- a/gdbsupport/selftest.cc +++ b/gdbsupport/selftest.cc @@ -101,7 +101,11 @@ run_tests (gdb::array_view filters, bool verbose) } catch (const gdb_exception_error &ex) { - debug_printf ("Self test failed: %s\n", ex.what ()); + if (ex.error == SELF_CHECK_FAILED_ERROR) + debug_printf ("FAIL: %s\n", ex.what ()); + else + debug_printf ("FAIL: unexpected exception: %s\n", ex.what ()); + failed.push_back (test.name.c_str ()); } @@ -138,4 +142,13 @@ all_selftests () return selftests_range (tests.cbegin (), tests.cend ()); } +/* See selftest.h. */ + +void +failed_selftest (const char *file, int line, const char *expression) +{ + throw_error (SELF_CHECK_FAILED_ERROR, "%s:%d: %s", file, line, + expression); +} + } /* namespace selftests */ diff --git a/gdbsupport/selftest.h b/gdbsupport/selftest.h index 2a1539827940..ddf925dd3524 100644 --- a/gdbsupport/selftest.h +++ b/gdbsupport/selftest.h @@ -83,14 +83,23 @@ extern void run_tests (gdb::array_view filters, /* Reset GDB or GDBserver's internal state. */ extern void reset (); + +/* Function called by macro SELF_CHECK when a selftest fails. Throw an + error with code SELF_CHECK_FAILED_ERROR describing the failure. */ + +[[noreturn]] void failed_selftest (const char *file, int line, + const char *expression); + } /* Check that VALUE is true, and, if not, throw an exception. */ #define SELF_CHECK(VALUE) \ - do { \ - if (!(VALUE)) \ - error (_("self-test failed at %s:%d"), __FILE__, __LINE__); \ - } while (0) + do \ + { \ + if (!(VALUE)) \ + selftests::failed_selftest (__FILE__, __LINE__, #VALUE); \ + } \ + while (0) #endif /* GDBSUPPORT_SELFTEST_H */ -- 2.56.0