From: Tom de Vries <tdevries@suse.de>
To: gdb-patches@sourceware.org
Subject: [PATCH 2/2] [gdb/cli] Fix wrapping for TERM=ansi
Date: Sat, 15 Apr 2023 08:09:05 +0200 [thread overview]
Message-ID: <20230415060905.18498-1-tdevries@suse.de> (raw)
[ This patch requires readline_hidden_col, introduced by this patch (
https://sourceware.org/pipermail/gdb-patches/2023-April/198877.html ). ]
Say we have:
...
$ echo $COLUMNS
40
...
With xterm, we claim to wrap after 40 chars:
...
$ TERM=xterm gdb
(gdb) show width
Number of characters gdb thinks are in a line is 40.
...
And with ansi, after 39 chars:
...
$ TERM=ansi gdb
(gdb) show width
Number of characters gdb thinks are in a line is 39.
...
Let's see if that's correct. First, let's note that the prompt prefix
"(gdb) " is 6 chars long:
...
123456
(gdb)
...
so we'll tag on starting with 7.
Let's try with xterm:
...
$ TERM=xterm gdb
(gdb) 7890123456789012345678901234567890
123
...
That looks as expected, wrapping occurs after 40 chars.
Now, let's try with ansi:
...
$ TERM=ansi gdb
(gdb) 78901234567890123456789012345678
90123
...
It looks like wrapping occurred after 38 instead of 39 chars.
This is caused by:
- readline detecting the screen width: 40,
- readline substracting one from the screen width because the ansi terminal
does not support autowrap, setting the readline screen width to 39,
- readline reporting 39 to gdb as screen width,
- gdb setting readline screen width to 39,
- again readline substracting one from the screen width, setting the readline
screen width to 38.
Fix this by taking readline_hidden_cols into account in set_screen_size.
PR cli/30346
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30346
Tested on x86_64-linux.
---
gdb/testsuite/gdb.base/wrap-line.exp | 104 +++++++++++++++++++++++++++
gdb/utils.c | 2 +-
2 files changed, 105 insertions(+), 1 deletion(-)
create mode 100644 gdb/testsuite/gdb.base/wrap-line.exp
diff --git a/gdb/testsuite/gdb.base/wrap-line.exp b/gdb/testsuite/gdb.base/wrap-line.exp
new file mode 100644
index 00000000000..2c4828c905b
--- /dev/null
+++ b/gdb/testsuite/gdb.base/wrap-line.exp
@@ -0,0 +1,104 @@
+# Copyright 2023 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+# Check that wrapping occurs at the expected location.
+
+# We set TERM on build, but we need to set it on host. That only works if
+# build == host.
+require {!is_remote host}
+
+# Test both ansi (no auto-wrap) and xterm (auto-wrap).
+set terms {ansi xterm}
+
+foreach_with_prefix term $terms {
+ save_vars { env(TERM) INTERNAL_GDBFLAGS } {
+
+ setenv TERM $term
+
+ # Let's use the width as determined by readline to get correct
+ # wrapping in the auto-wrap case. Avoid "set width" argument.
+ set INTERNAL_GDBFLAGS \
+ [string map {{-iex "set width 0"} ""} $INTERNAL_GDBFLAGS]
+
+ # Avoid "set width" in default_gdb_start.
+ gdb_exit
+ gdb_spawn
+ }
+
+ set test "initial prompt"
+ gdb_test_multiple "" $test {
+ -re "^$gdb_prompt $" {
+ pass "$test"
+ }
+ }
+
+ if { ! [readline_is_used] } {
+ continue
+ }
+
+ set width 0
+ set show_width_re \
+ "Number of characters gdb thinks are in a line is ($decimal)\\."
+ gdb_test_multiple "show width" "" {
+ -re -wrap $show_width_re {
+ set width $expect_out(1,string)
+ pass $gdb_test_name
+ }
+ }
+
+ if { $width == 0 } {
+ continue
+ }
+
+ # New prompt, but avoid emitting a pass in order to avoid ending the line
+ # after the prompt in gdb.log. This make it a bit easier in gdb.log to
+ # understand where wrapping occurred.
+ gdb_test_multiple "print 1" "" {
+ -re -wrap " = 1" {
+ }
+ }
+
+ # Take into account that the prompt also takes space.
+ set prefix [string length "(gdb) "]
+ set start [expr $prefix + 1]
+
+ # Print chars without wrapping.
+ set i $start
+ while { 1 } {
+ send_gdb [expr $i % 10]
+ if { $i == $width } {
+ break
+ }
+ incr i
+ }
+
+ # Now print the first char we expect to wrap.
+ send_gdb "W"
+
+ # Generate a prompt.
+ send_gdb "\003"
+
+ # Note the difference between autowrap and no autowrap. In the autowrap
+ # case, readline doesn't emit a '\n', the terminal takes care of that.
+ if { $term == "xterm" } {
+ # xterm, autowrap.
+ set re "^$start\[^\r\n\]* \rWQuit"
+ } else {
+ # ansi, no autowrap.
+ set re "^$start\[^\r\n\]*\r\n\rWQuit"
+ }
+
+ gdb_test "" $re "wrap"
+}
diff --git a/gdb/utils.c b/gdb/utils.c
index 82138a1fc2c..eda09cf9c6b 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -1228,7 +1228,7 @@ static void
set_screen_size (void)
{
int rows = lines_per_page;
- int cols = chars_per_line;
+ int cols = chars_per_line + readline_hidden_cols;
/* If we get 0 or negative ROWS or COLS, treat as "infinite" size.
A negative number can be seen here with the "set width/height"
--
2.35.3
next reply other threads:[~2023-04-15 6:08 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-04-15 6:09 Tom de Vries [this message]
2023-04-15 8:10 ` Tom de Vries
2023-05-02 13:40 ` 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=20230415060905.18498-1-tdevries@suse.de \
--to=tdevries@suse.de \
--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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).