Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH] [gdb/remote] Reimplement escape_buffer
@ 2026-03-20 14:09 Tom de Vries
  2026-03-20 17:29 ` Tom Tromey
  0 siblings, 1 reply; 3+ messages in thread
From: Tom de Vries @ 2026-03-20 14:09 UTC (permalink / raw)
  To: gdb-patches

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 std::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.

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
---
 gdb/remote.c | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)

diff --git a/gdb/remote.c b/gdb/remote.c
index e84a0dc578b..d525fbe407b 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -84,6 +84,8 @@
 #include "cli/cli-style.h"
 #include "gdbsupport/remote-args.h"
 #include "finish-thread-state.h"
+#include <sstream>
+#include <iomanip>
 
 /* The remote target.  */
 
@@ -10509,15 +10511,33 @@ 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;
+  std::ostringstream s;
 
-  stb.putstrn (buf, n, '\\');
-  return stb.release ();
+  for (int i = 0; i < n; ++i)
+    {
+      char c = buf[i];
+      if (isprint (c))
+	{
+	  str += c;
+	  continue;
+	}
+
+      s.str ("");
+
+      /* Print 3-wide leading zero octal.  */
+      s << std::oct << std::setw (3) << std::setfill ('0') << (unsigned int)c;
+
+      str.append ("\\");
+      str.append (s.str ());
+    }
+
+  return str;
 }
 
 int

base-commit: ebe694f0b28a2cfbb30aaf127eb7200e68871a5a
-- 
2.51.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] [gdb/remote] Reimplement escape_buffer
  2026-03-20 14:09 [PATCH] [gdb/remote] Reimplement escape_buffer Tom de Vries
@ 2026-03-20 17:29 ` Tom Tromey
  2026-03-21  9:45   ` Tom de Vries
  0 siblings, 1 reply; 3+ messages in thread
From: Tom Tromey @ 2026-03-20 17:29 UTC (permalink / raw)
  To: Tom de Vries; +Cc: gdb-patches

>>>>> "Tom" == Tom de Vries <tdevries@suse.de> writes:

Tom> +#include <sstream>

I don't think we want C++ streams generally.
At least we haven't so far.

Tom> +      if (isprint (c))

gdb generally uses c_isprint, not the ctype stuff.

thanks,
Tom

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] [gdb/remote] Reimplement escape_buffer
  2026-03-20 17:29 ` Tom Tromey
@ 2026-03-21  9:45   ` Tom de Vries
  0 siblings, 0 replies; 3+ messages in thread
From: Tom de Vries @ 2026-03-21  9:45 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

On 3/20/26 6:29 PM, Tom Tromey wrote:
>>>>>> "Tom" == Tom de Vries <tdevries@suse.de> writes:
> 
> Tom> +#include <sstream>
> 
> I don't think we want C++ streams generally.
> At least we haven't so far.
> 
> Tom> +      if (isprint (c))
> 
> gdb generally uses c_isprint, not the ctype stuff.
> 

Hi Tom,

thanks for the review.

I've fixed these two things in a v2 ( 
https://sourceware.org/pipermail/gdb-patches/2026-March/226119.html ).

Thanks,
- Tom


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-03-21  9:46 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-03-20 14:09 [PATCH] [gdb/remote] Reimplement escape_buffer Tom de Vries
2026-03-20 17:29 ` Tom Tromey
2026-03-21  9:45   ` Tom de Vries

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox