Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH] Do not write negative PID or TID in remote protocol
@ 2026-03-11 20:21 Tom Tromey
  2026-03-12 19:42 ` Kevin Buettner
  0 siblings, 1 reply; 2+ messages in thread
From: Tom Tromey @ 2026-03-11 20:21 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

Currently both gdb and gdbserver can write a negative number for the
PID or TID.  However, the only negative value that really makes sense
is the special case of "-1" -- in other cases if the PID or TID has
the high bit set, it should still be written as a positive number.

This patch attempts to fix the bug.  I am not really sure how to test
it.

As an aside there seems to be more low-level RSP code that could be
shared here.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=25111
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=33979
---
 gdb/remote.c              | 19 ++++++++-----------
 gdbserver/remote-utils.cc | 12 ++++++------
 2 files changed, 14 insertions(+), 17 deletions(-)

diff --git a/gdb/remote.c b/gdb/remote.c
index 88b06e688bc..54aeaba0ce5 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -3684,22 +3684,19 @@ static int remote_newthread_step (threadref *ref, void *context);
 char *
 remote_target::write_ptid (char *buf, const char *endbuf, ptid_t ptid)
 {
-  ptid_t::pid_type pid;
-  ptid_t::lwp_type lwp;
-
   if (m_features.remote_multi_process_p ())
     {
-      pid = ptid.pid ();
-      if (pid < 0)
-	buf += xsnprintf (buf, endbuf - buf, "p-%x.", -pid);
+      ptid_t::pid_type pid = ptid.pid ();
+      if (pid == -1)
+	buf += xsnprintf (buf, endbuf - buf, "p-1.");
       else
-	buf += xsnprintf (buf, endbuf - buf, "p%x.", pid);
+	buf += xsnprintf (buf, endbuf - buf, "p%x.", (unsigned) pid);
     }
-  lwp = ptid.lwp ();
-  if (lwp < 0)
-    buf += xsnprintf (buf, endbuf - buf, "-%lx", -lwp);
+  ptid_t::lwp_type lwp = ptid.lwp ();
+  if (lwp == -1)
+    buf += xsnprintf (buf, endbuf - buf, "-1");
   else
-    buf += xsnprintf (buf, endbuf - buf, "%lx", lwp);
+    buf += xsnprintf (buf, endbuf - buf, "%lx", (unsigned long) lwp);
 
   return buf;
 }
diff --git a/gdbserver/remote-utils.cc b/gdbserver/remote-utils.cc
index 7671206ebc1..a0ae04bd632 100644
--- a/gdbserver/remote-utils.cc
+++ b/gdbserver/remote-utils.cc
@@ -525,16 +525,16 @@ write_ptid (char *buf, ptid_t ptid)
   if (cs.multi_process)
     {
       pid = ptid.pid ();
-      if (pid < 0)
-	buf += sprintf (buf, "p-%x.", -pid);
+      if (pid == -1)
+	buf += sprintf (buf, "p-1.");
       else
-	buf += sprintf (buf, "p%x.", pid);
+	buf += sprintf (buf, "p%x.", (unsigned) pid);
     }
   lwp = ptid.lwp ();
-  if (lwp < 0)
-    buf += sprintf (buf, "-%lx", -lwp);
+  if (lwp == -1)
+    buf += sprintf (buf, "-1");
   else
-    buf += sprintf (buf, "%lx", lwp);
+    buf += sprintf (buf, "%lx", (unsigned long) lwp);
 
   return buf;
 }

base-commit: b18149c8e933d8539b8a49c5d200b1ace513e339
-- 
2.53.0


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

end of thread, other threads:[~2026-03-12 19:42 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-03-11 20:21 [PATCH] Do not write negative PID or TID in remote protocol Tom Tromey
2026-03-12 19:42 ` Kevin Buettner

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