From: Tom de Vries <tdevries@suse.de>
To: Simon Marchi <simon.marchi@efficios.com>,
gcc-patches@sourceware.org, gdb-patches@sourceware.org
Subject: Re: [PATCH 2/6] gdbsupport/selftest: print expression when selftest fails and re-format
Date: Fri, 20 Nov 2026 10:02:36 +0100 [thread overview]
Message-ID: <b46cbfcd-7c6c-44c9-ba84-599cc4dd5a9a@suse.de> (raw)
In-Reply-To: <20261019165929.792697-3-simon.marchi@efficios.com>
On 10/19/26 6:58 PM, Simon Marchi wrote:
> 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.
>
Hi Simon,
thanks for doing this.
I think there are three independent changes in here:
- use #VALUE
- SELF_CHECK_FAILED_ERROR
- use FAIL
I like the first two, a lot.
The last I also like, it makes the failure stand out more. An LLM
review pointed out that there may be some confusion due to using the
dejagnu FAIL message, but I don't think that will be a problem.
I got two remarks inline below.
With those fixed, LGTM.
Approved-By: Tom de Vries <tdevries@suse.de>
Thanks,
- Tom
> 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<const char *const> 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<const char *const> 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);
> +
Missing extern.
> }
>
> /* 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); \
This should use a :: prefix, otherwise the build fails when compiling
ada-exp-parser.c.
> + } \
> + while (0)
>
> #endif /* GDBSUPPORT_SELFTEST_H */
next prev parent reply other threads:[~2026-11-20 9:03 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-19 16:58 [PATCH 0/6] Make SELF_CHECK print operands of comparisons Simon Marchi
2026-10-19 16:58 ` [PATCH 1/6] gdb/unittests: catch gdb_exception_error in scoped_mmap test Simon Marchi
2026-11-20 8:24 ` Tom de Vries
2026-10-19 16:58 ` [PATCH 2/6] gdbsupport/selftest: print expression when selftest fails and re-format Simon Marchi
2026-11-20 9:02 ` Tom de Vries [this message]
2026-10-19 16:58 ` [PATCH 3/6] include/diagnostics.h: add DIAGNOSTIC_IGNORE_PARENTHESES Simon Marchi
2026-11-20 9:09 ` Tom de Vries
2026-10-19 16:58 ` [PATCH 4/6] gdb/gmp-utils: rename gdb_mp{z,q,f}::str to to_string Simon Marchi
2026-11-20 9:58 ` [PATCH 4/6] gdb/gmp-utils: rename gdb_mp{z, q, f}::str " Tom de Vries
2026-10-19 16:58 ` [PATCH 5/6] gdbsupport/selftest: print operand values when a SELF_CHECK fails Simon Marchi
2026-10-27 14:13 ` Tom Tromey
2026-10-27 17:06 ` Simon Marchi
2026-11-20 12:08 ` Tom de Vries
2026-11-21 4:12 ` Simon Marchi
2026-10-19 16:58 ` [PATCH 6/6] gdb: remove manual printing of values in some selftests Simon Marchi
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=b46cbfcd-7c6c-44c9-ba84-599cc4dd5a9a@suse.de \
--to=tdevries@suse.de \
--cc=gcc-patches@sourceware.org \
--cc=gdb-patches@sourceware.org \
--cc=simon.marchi@efficios.com \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox