Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH v4 5/5] Fix 32-bit GPR and SPR register reads/writes in pthdb callbacks.
@ 2026-10-05 13:36 Aditya Vidyadhar Kamath
  0 siblings, 0 replies; only message in thread
From: Aditya Vidyadhar Kamath @ 2026-10-05 13:36 UTC (permalink / raw)
  To: ulrich.weigand, simon.marchi, tom
  Cc: gdb-patches, Aditya.Kamath1, sangamesh.swamy, Aditya Vidyadhar Kamath

From: Aditya Vidyadhar Kamath <aditya.kamath1@ibm.com>

This patch does two simple changes mentioned in each paragraph below:

When a 32-bit thread's GPRs are read via pdc_read_regs, a raw memcpy
from a uint32_t[32] array into pthdb_context_t::gpr which holds
uint64_t elements was silently truncating or misinterpreting data.
Replace the memcpy with an element-wise assignment so each 32-bit
value is correctly zero-extended into the 64-bit context field.
Similarly, the SPRS read for 32-bit threads was copying the entire
struct ptsprs over &context->msr, clobbering adjacent context fields.
Replace the memcpy with explicit field-by-field assignments.

For writes in pdc_write_regs, restore the arch64 branch distinction:
1: 64-bit: pass context->gpr / &context->msr directly as before.
2: 32-bit GPRs: truncate each context->gpr[i] (uint64_t) into a local
  uint32_t gprs32[] array before calling ptrace64aix.
3: 32-bit SPRs: pack a local struct ptsprs from the individual context
  fields before calling ptrace64aix.
---
 gdb/aix-thread.c | 42 ++++++++++++++++++++++++++++++++++++++----
 1 file changed, 38 insertions(+), 4 deletions(-)

diff --git a/gdb/aix-thread.c b/gdb/aix-thread.c
index 4826e33265c..f4693702804 100644
--- a/gdb/aix-thread.c
+++ b/gdb/aix-thread.c
@@ -411,7 +411,8 @@ pdc_read_regs (pthdb_user_t user_current_pid,
 	{
 	  if (!ptrace64aix (PTT_READ_GPRS, tid, (long long) gprs32, 0, NULL))
 	    memset (gprs32, 0, sizeof (gprs32));
-	  memcpy (context->gpr, gprs32, sizeof (gprs32));
+	  for (int i = 0; i < ppc_num_gprs; i++)
+	    context->gpr[i] = gprs32[i];
 	}
     }
 
@@ -436,7 +437,14 @@ pdc_read_regs (pthdb_user_t user_current_pid,
 	{
 	  if (!ptrace64aix (PTT_READ_SPRS, tid, (long long) &sprs32, 0, NULL))
 	    memset (&sprs32, 0, sizeof (sprs32));
-	  memcpy (&context->msr, &sprs32, sizeof (sprs32));
+	  context->iar = sprs32.pt_iar;
+	  context->msr = sprs32.pt_msr;
+	  context->cr = sprs32.pt_cr;
+	  context->lr = sprs32.pt_lr;
+	  context->ctr = sprs32.pt_ctr;
+	  context->xer = sprs32.pt_xer;
+	  context->fpscr = sprs32.pt_fpscr;
+	  context->mq = sprs32.pt_mq;
 	}
     }
 
@@ -486,7 +494,17 @@ pdc_write_regs (pthdb_user_t user_current_pid,
 
   /* General-purpose registers.  */
   if (flags & PTHDB_FLAG_GPRS)
-    ptrace64aix (PTT_WRITE_GPRS, tid, (long long) context->gpr, 0, NULL);
+    {
+      if (data->arch64)
+	ptrace64aix (PTT_WRITE_GPRS, tid, (long long) context->gpr, 0, NULL);
+      else
+	{
+	  uint32_t gprs32[ppc_num_gprs];
+	  for (int i = 0; i < ppc_num_gprs; i++)
+	    gprs32[i] = context->gpr[i];
+	  ptrace64aix (PTT_WRITE_GPRS, tid, (long long) gprs32, 0, NULL);
+	}
+     }
 
   /* Floating-point registers.  */
   if (flags & PTHDB_FLAG_FPRS)
@@ -494,7 +512,23 @@ pdc_write_regs (pthdb_user_t user_current_pid,
 
   /* Special-purpose registers.  */
   if (flags & PTHDB_FLAG_SPRS)
-    ptrace64aix (PTT_WRITE_SPRS, tid, (long long) &context->msr, 0, NULL);
+    {
+      if (data->arch64)
+	ptrace64aix (PTT_WRITE_SPRS, tid, (long long) &context->msr, 0, NULL);
+      else
+	{
+	   struct ptsprs sprs32;
+	   sprs32.pt_iar = context->iar;
+	   sprs32.pt_msr = context->msr;
+	   sprs32.pt_cr = context->cr;
+	   sprs32.pt_lr = context->lr;
+	   sprs32.pt_ctr = context->ctr;
+	   sprs32.pt_xer = context->xer;
+	   sprs32.pt_fpscr = context->fpscr;
+	   sprs32.pt_mq = context->mq;
+	   ptrace64aix (PTT_WRITE_SPRS, tid, (long long) &sprs32, 0, NULL);
+	}
+    }
 
   /* vector registers.  */
   if (__power_vmx () && (flags & PTHDB_FLAG_REGS))
-- 
2.51.2


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-10-05 13:38 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-05 13:36 [PATCH v4 5/5] Fix 32-bit GPR and SPR register reads/writes in pthdb callbacks Aditya Vidyadhar Kamath

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