public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom de Vries <tdevries@suse.de>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: Re: [PATCH 8/8] [gdb/tui] Fix TUI for TERM=ansi
Date: Tue, 25 Apr 2023 09:09:52 +0200	[thread overview]
Message-ID: <b79b9fd1-3c1e-8c32-c2f7-0dfc574471fa@suse.de> (raw)
In-Reply-To: <cc331915-bb8f-d8d2-f041-987219eaed18@suse.de>

[-- Attachment #1: Type: text/plain, Size: 178 bytes --]

On 4/18/23 08:10, Tom de Vries via Gdb-patches wrote:
> So, this patch could be simplified by just using _rl_term_autowrap.

This version uses _rl_term_autowrap.

Thanks,
- Tom


[-- Attachment #2: 0001-gdb-tui-Fix-TUI-for-TERM-ansi.patch --]
[-- Type: text/x-patch, Size: 5502 bytes --]

From 706916401057521955b9f74bb4079e2619c52039 Mon Sep 17 00:00:00 2001
From: Tom de Vries <tdevries@suse.de>
Date: Wed, 12 Apr 2023 14:44:42 +0200
Subject: [PATCH] [gdb/tui] Fix TUI for TERM=ansi

With TERM=ansi, when resizing a TUI window from LINES/COLUMNS 31/118
(maximized) to 20/78 (de-maximized), I get a garbled screen and a message:
...
@@ resize done 0, size = 77x20
...
with the resulting width being 77 instead of the expected 78.

[ The discrepancy also manifests in CLI, filed as PR30346. ]

The discrepancy comes from tui_resize_all, where we ask readline for the
screen size:
...
   rl_get_screen_size (&screenheight, &screenwidth);
...

As it happens, when TERM is set to ansi, readline decides that the terminal
cannot auto-wrap lines, and reserves one column to deal with that, and as a
result reports back one less than the actual screen width:
...
$ echo $COLUMNS
78
$ TERM=xterm gdb -ex "show width" -ex q
Number of characters gdb thinks are in a line is 78.
$ TERM=ansi  gdb -ex "show width" -ex q
Number of characters gdb thinks are in a line is 77.
...

In tui_resize_all, we need the actual screen width, and using a screenwidth of
one less than the actual value garbles the screen.

This is currently not causing trouble in testing because we have a workaround
in place in proc Term::resize.

Fix this by:
- detecting when readline will report back less than the actual screen width,
- accordingly setting a new variable readline_hidden_cols,
- using readline_hidden_cols in tui_resize_all to fix the resize problem, and
- removing the workaround in Term::resize.

The test-case gdb.tui/empty.exp serves as regression test.

Tested on x86_64-linux.

PR tui/30337
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30337
---
 gdb/testsuite/lib/tuiterm.exp | 10 ++--------
 gdb/tui/tui-win.c             |  3 +++
 gdb/utils.c                   | 21 +++++++++++++++++++++
 gdb/utils.h                   |  7 +++++++
 4 files changed, 33 insertions(+), 8 deletions(-)

diff --git a/gdb/testsuite/lib/tuiterm.exp b/gdb/testsuite/lib/tuiterm.exp
index d8a99d2798a..f59a66a0958 100644
--- a/gdb/testsuite/lib/tuiterm.exp
+++ b/gdb/testsuite/lib/tuiterm.exp
@@ -1122,16 +1122,10 @@ namespace eval Term {
 	# explicit here.  This also simplifies waiting for the redraw.
 	_do_resize $rows $_cols
 	stty rows $_rows < $::gdb_tty_name
-	# Due to the strange column resizing behavior, and because we
-	# don't care about this intermediate resize, we don't check
-	# the size and the "@@ " prefix here.
-	wait_for "resize done $_resize_count"
+	wait_for "@@ resize done $_resize_count, size = ${_cols}x${rows}"
 	incr _resize_count
-	# Somehow the number of columns transmitted to gdb is one less
-	# than what we request from expect.  We hide this weird
-	# details from the caller.
 	_do_resize $_rows $cols
-	stty columns [expr {$_cols + 1}] < $::gdb_tty_name
+	stty columns $_cols < $::gdb_tty_name
 	wait_for "@@ resize done $_resize_count, size = ${_cols}x${rows}"
 	incr _resize_count
     }
diff --git a/gdb/tui/tui-win.c b/gdb/tui/tui-win.c
index 3b17cb8dd29..7186fb97d68 100644
--- a/gdb/tui/tui-win.c
+++ b/gdb/tui/tui-win.c
@@ -36,6 +36,7 @@
 #include "gdbsupport/event-loop.h"
 #include "gdbcmd.h"
 #include "async-event.h"
+#include "utils.h"
 
 #include "tui/tui.h"
 #include "tui/tui-io.h"
@@ -528,6 +529,8 @@ tui_resize_all (void)
   int screenheight, screenwidth;
 
   rl_get_screen_size (&screenheight, &screenwidth);
+  screenwidth += readline_hidden_cols;
+
   width_diff = screenwidth - tui_term_width ();
   height_diff = screenheight - tui_term_height ();
   if (height_diff || width_diff)
diff --git a/gdb/utils.c b/gdb/utils.c
index b5bb84ce85d..282c4ca70b9 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -1116,6 +1116,14 @@ static bool filter_initialized = false;
 
 \f
 
+/* See readline's rlprivate.h.  */
+
+EXTERN_C int _rl_term_autowrap;
+
+/* See utils.h.  */
+
+int readline_hidden_cols = 0;
+
 /* Initialize the number of lines per page and chars per line.  */
 
 void
@@ -1144,6 +1152,19 @@ init_page_info (void)
 
       /* Get the screen size from Readline.  */
       rl_get_screen_size (&rows, &cols);
+
+      /* Readline:
+	 - ignores the COLUMNS variable when detecting screen width
+	   (because rl_prefer_env_winsize defaults to 0)
+	 - puts the detected screen width in the COLUMNS variable
+	   (because rl_change_environment defaults to 1)
+	 - may report one less than the detected screen width in
+	   rl_get_screen_size (when _rl_term_autowrap == 0).
+	 We could set readline_hidden_cols by comparing COLUMNS to cols as
+	 returned by rl_get_screen_size, but instead simply use
+	 _rl_term_autowrap.  */
+      readline_hidden_cols = _rl_term_autowrap ? 0 : 1;
+
       lines_per_page = rows;
       chars_per_line = cols;
 
diff --git a/gdb/utils.h b/gdb/utils.h
index a383036bcfe..29ff376bb52 100644
--- a/gdb/utils.h
+++ b/gdb/utils.h
@@ -335,4 +335,11 @@ extern void copy_bitwise (gdb_byte *dest, ULONGEST dest_offset,
 			  const gdb_byte *source, ULONGEST source_offset,
 			  ULONGEST nbits, int bits_big_endian);
 
+/* When readline decides that the terminal cannot auto-wrap lines, it reduces
+   the width of the reported screen width by 1.  This variable indicates
+   whether that's the case or not, allowing us to add it back where
+   necessary.  See _rl_term_autowrap in readline/terminal.c.  */
+
+extern int readline_hidden_cols;
+
 #endif /* UTILS_H */

base-commit: 538edc49dc610b987f8929434f883c8bbc211be8
-- 
2.35.3


  reply	other threads:[~2023-04-25  7:09 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-13 14:08 [PATCH 0/8] [gdb/testsuite] Fix timeouts in TUI tests Tom de Vries
2023-04-13 14:08 ` [PATCH 1/8] [gdb/testsuite] Add warning for timeout in accept_gdb_output Tom de Vries
2023-04-13 14:08 ` [PATCH 2/8] [gdb/testsuite] Add debug prints in Term::wait_for Tom de Vries
2023-04-13 14:08 ` [PATCH 3/8] [gdb/testsuite] Fix timeout in gdb.tui/corefile-run.exp Tom de Vries
2023-04-13 14:08 ` [PATCH 4/8] [gdb/testsuite] Fix timeout in gdb.tui/main.exp Tom de Vries
2023-04-13 14:08 ` [PATCH 5/8] [gdb/testsuite] Fix timeout in gdb.tui/new-layout.exp Tom de Vries
2023-04-13 14:08 ` [PATCH 6/8] [gdb/testsuite] Fix timeout in gdb.tui/completion.exp Tom de Vries
2023-04-13 14:08 ` [PATCH 7/8] [gdb/testsuite] Fix timeout in gdb.tui/empty.exp Tom de Vries
2023-04-13 14:08 ` [PATCH 8/8] [gdb/tui] Fix TUI for TERM=ansi Tom de Vries
2023-04-13 14:17   ` Tom de Vries
2023-04-14 11:02     ` Tom de Vries
2023-04-18  6:10       ` Tom de Vries
2023-04-25  7:09         ` Tom de Vries [this message]
2023-04-30 11:08           ` Tom de Vries
2023-04-25  6:35 ` [PATCH 0/8] [gdb/testsuite] Fix timeouts in TUI tests Tom de Vries

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=b79b9fd1-3c1e-8c32-c2f7-0dfc574471fa@suse.de \
    --to=tdevries@suse.de \
    --cc=gdb-patches@sourceware.org \
    --cc=tom@tromey.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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).