Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom de Vries <tdevries@suse.de>
To: gdb-patches@sourceware.org
Subject: [PATCH v2] [gdb/remote] Reimplement escape_buffer
Date: Sat, 21 Mar 2026 10:44:33 +0100	[thread overview]
Message-ID: <20260321094433.3298268-1-tdevries@suse.de> (raw)

With Fedora RH, using Tcl 9.0.2, I run into:
...
(gdb) break -q main^M
[remote] Sending packet: $qXfer:auxv:read::0,1000#6b^M
[remote] Packet received: l!\000\000\000\000\000\000\000\000 ERROR: \
  i_read(spawn_id fd=9): invalid or incomplete multibyte or wide character
  ...
UNRESOLVED: gdb.server/bkpt-other-inferior.exp: inf 2: set breakpoint
...

For reference, on openSUSE Leap 16.0, with Tcl 8.6.15 the initial part of the
problematic string looks like:
...
[remote] Packet received: l!\000\000\000\000\000\000\000\000`ü÷ÿ\177\000...
...

With Tcl 9.0, something (dejagnu/expect/gdb testsuite infrastructure)
has problems with gdb IO containing non-utf-8 chars.

Having said that, I don't think having chars like 'ü' in the debug output is
particularly helpful.

The easiest and most robust approach would be to just print octal escape
sequences, but that would mean we no longer print 'l', which indicates to us
what kind of qXfer reply we're looking at [1].

Reimplement function escape_buffer in gdb/remote.c, to print c_isprint chars
like the actual chars, and others like octal escape sequences, getting us
instead:
...
[remote] Packet received: \
  l!\000\000\000\000\000\000\000\000`\374\367\377\177\000...
...

Note that this is ambiguous, because one char '\0' and 4 chars '\\' '0' '0' '0'
are printed the same: \000, but I guess that's a pre-existing problem.

This also fixes test-case gdb.python/py-send-packet.exp, where we run into a
similar problem with the "Sending packet:" line.

Tested on x86_64-linux, with Tcl 9.0.2 and 8.6.15.

A v1 submitted was submitted here [2].

Changes in v2:
- use c_isprint instead of std::isprint
- use string_appendf instead of std::ostringstream

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34012

[1] https://sourceware.org/gdb/current/onlinedocs/gdb.html/General-Query-Packets.html#index-qXfer-packet
[2] v1 https://sourceware.org/pipermail/gdb-patches/2026-March/226110.html
---
 gdb/remote.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/gdb/remote.c b/gdb/remote.c
index e84a0dc578b..8c3d60bd326 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -10509,15 +10509,27 @@ remote_target::remote_serial_send_break ()
 }
 
 /* Return a string representing an escaped version of BUF, of len N.
-   E.g. \n is converted to \\n, \t to \\t, etc.  */
+   E.g. '\0' is converted to "\000", etc.  */
 
 static std::string
 escape_buffer (const char *buf, int n)
 {
-  string_file stb;
+  std::string str;
 
-  stb.putstrn (buf, n, '\\');
-  return stb.release ();
+  for (int i = 0; i < n; ++i)
+    {
+      char c = buf[i];
+      if (c_isprint (c))
+	{
+	  str += c;
+	  continue;
+	}
+
+      /* Append 3-wide leading zero octal \ooo.  */
+      string_appendf (str, "\\%03hho", c);
+    }
+
+  return str;
 }
 
 int

base-commit: c3d4baadd233635c13c306d9fdff3342710633ed
-- 
2.51.0


             reply	other threads:[~2026-03-21  9:45 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-21  9:44 Tom de Vries [this message]
2026-03-23 14:12 ` Tom Tromey
2026-03-23 17:20   ` Tom de Vries
2026-03-23 18:28     ` Tom Tromey
2026-03-24 15:34       ` 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=20260321094433.3298268-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