From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH 1/5] Ensure tgetent is called before tgetnum
Date: Fri, 12 Jun 2026 08:25:21 -0600 [thread overview]
Message-ID: <20260612-submit-zebra-style-v1-1-cfd00b5c984c@tromey.com> (raw)
In-Reply-To: <20260612-submit-zebra-style-v1-0-cfd00b5c984c@tromey.com>
A patch in this series will cause gdb_get_ncolors to be called earlier
in startup. I was surprised to find that this function was failing
when gdb was run from the test suite, and eventually I tracked this
down to the fact that gdb_get_ncolors calls tgetnum but does not first
ensure that tgetent is called.
This patch changes the code to ensure tgetent is always called first.
I believe tgetent is idempotent so this is safe to do.
The new test case is distilled from the test that was failing for me.
Note that it does not actually fail without this patch -- the current
gdb passes here. However by the end of the series this test would
fail without this patch, so it just assures there won't be a failure
here.
---
gdb/mingw-hdep.c | 6 +-----
gdb/posix-hdep.c | 6 +-----
gdb/testsuite/gdb.base/style.exp | 14 ++++++++++++++
gdb/ui-style.c | 30 ++++++++++++++++++++++++++++++
gdb/ui-style.h | 3 +++
5 files changed, 49 insertions(+), 10 deletions(-)
diff --git a/gdb/mingw-hdep.c b/gdb/mingw-hdep.c
index 90ebe252f57..d4607b1214f 100644
--- a/gdb/mingw-hdep.c
+++ b/gdb/mingw-hdep.c
@@ -273,11 +273,7 @@ mingw_deinitialize_console ()
int
gdb_get_ncolors ()
{
- /* ncurses versions prior to 6.1 (and other curses
- implementations) declare the tgetnum argument to be
- 'char *', so we need the const_cast, since C++ will not
- implicitly convert. */
- int nc = tgetnum (const_cast<char*> ("Co"));
+ int nc = basic_gdb_get_ncolors ();
/* MS-Windows terminal generally doesn't have "Co" in its terminfo,
but always supports at least 8 colors. */
if (nc <= 0)
diff --git a/gdb/posix-hdep.c b/gdb/posix-hdep.c
index b9aff9ede1f..a87d55a3957 100644
--- a/gdb/posix-hdep.c
+++ b/gdb/posix-hdep.c
@@ -46,11 +46,7 @@ gdb_console_fputs (const char *buf, FILE *f)
int
gdb_get_ncolors ()
{
- /* ncurses versions prior to 6.1 (and other curses
- implementations) declare the tgetnum argument to be
- 'char *', so we need the const_cast, since C++ will not
- implicitly convert. */
- return tgetnum (const_cast<char*> ("Co"));
+ return basic_gdb_get_ncolors ();
}
/* See inferior.h. */
diff --git a/gdb/testsuite/gdb.base/style.exp b/gdb/testsuite/gdb.base/style.exp
index 8a49d1c5973..eb1cdf46112 100644
--- a/gdb/testsuite/gdb.base/style.exp
+++ b/gdb/testsuite/gdb.base/style.exp
@@ -1086,6 +1086,18 @@ proc test_logging_styling {} {
"No symbol table is loaded\\. Use the \"[style file command]\" command\\."
}
+# Test that colors "round trip" in ANSI mode.
+proc test_ansi_round_trip {} {
+ with_test_prefix "round-trip" {
+ with_ansi_styling_terminal {
+ clean_restart
+ gdb_test_no_output "set style address background red"
+ gdb_test "show style address background" \
+ "The .*address.* background color is: red"
+ }
+ }
+}
+
# Check to see if the Python styling of disassembler output is
# expected or not, this styling requires Python support in GDB, and
# the Python pygments module to be available.
@@ -1133,3 +1145,5 @@ test_pagination_prompt_styling
test_pagination_continue_styling
test_finish_styling
test_logging_styling
+
+test_ansi_round_trip
diff --git a/gdb/ui-style.c b/gdb/ui-style.c
index ab2b0ad5de4..3dd26fbc79a 100644
--- a/gdb/ui-style.c
+++ b/gdb/ui-style.c
@@ -578,6 +578,36 @@ examine_ansi_escape (const char *buf, int *n_read)
/* See ui-style.h. */
+int
+basic_gdb_get_ncolors ()
+{
+ static std::optional<int> result;
+
+ if (!result.has_value ())
+ {
+ const char *term_name = getenv ("TERM");
+ if (term_name == nullptr)
+ result = -1;
+ else
+ {
+ char desc[4096];
+ /* Ignore the result here. Some versions of termcap are
+ confused about this. */
+ tgetent (desc, term_name);
+
+ /* ncurses versions prior to 6.1 (and other curses
+ implementations) declare the tgetnum argument to be
+ 'char *', so we need the const_cast, since C++ will not
+ implicitly convert. */
+ result = tgetnum (const_cast<char*> ("Co"));
+ }
+ }
+
+ return *result;
+}
+
+/* See ui-style.h. */
+
const std::vector<color_space> &
colorsupport ()
{
diff --git a/gdb/ui-style.h b/gdb/ui-style.h
index fc40b93709d..52f5a5a104d 100644
--- a/gdb/ui-style.h
+++ b/gdb/ui-style.h
@@ -51,6 +51,9 @@ extern const char * color_space_name (color_space c);
/* Cast C to RESULT and return true if it's value is valid; false otherwise. */
extern bool color_space_safe_cast (color_space *result, long c);
+/* Basic implementation of gdb_get_ncolors that checks tgetent. */
+extern int basic_gdb_get_ncolors ();
+
/* Get the number of colors supported by the terminal where GDB is running. */
extern int gdb_get_ncolors ();
--
2.49.0
next prev parent reply other threads:[~2026-06-12 14:26 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-12 14:25 [PATCH 0/5] Add zebra-striping to CLI tables Tom Tromey
2026-06-12 14:25 ` Tom Tromey [this message]
2026-06-12 14:25 ` [PATCH 2/5] Make two ui_out methods 'private' Tom Tromey
2026-06-12 14:25 ` [PATCH 3/5] Allow "phony" ui-out tables Tom Tromey
2026-06-12 14:25 ` [PATCH 4/5] Add ui_file_style::merge Tom Tromey
2026-06-12 14:25 ` [PATCH 5/5] Add zebra-striping to CLI table display Tom Tromey
2026-06-12 15:14 ` Eli Zaretskii
2026-06-13 14:28 ` Matt Rice
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=20260612-submit-zebra-style-v1-1-cfd00b5c984c@tromey.com \
--to=tom@tromey.com \
--cc=gdb-patches@sourceware.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox