From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id AeLrGEUUzmpU7gAAWB0awg (envelope-from ) for ; Tue, 13 Oct 2026 07:21:41 -0400 Authentication-Results: simark.ca; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=tLJmt12+; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 4E5DF1E051; Tue, 13 Oct 2026 07:21:41 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-2.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 1435D1E04E for ; Tue, 13 Oct 2026 07:21:39 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3548B4BA23DE for ; Tue, 13 Oct 2026 11:21:38 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3548B4BA23DE Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=tLJmt12+ Received: from mail-pf1-x431.google.com (mail-pf1-x431.google.com [IPv6:2607:f8b0:4864:20::431]) by sourceware.org (Postfix) with ESMTPS id 25F994BA23FA for ; Tue, 13 Oct 2026 11:20:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 25F994BA23FA Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 25F994BA23FA Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::431 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1791890440; cv=none; b=eNkSRNOD1+tsIwDkoz1F2SU0dW3pG2XjtVRpSQ8ALc/nRFGjc3WG8Wb2CCzQ+2s7nXHlbSe86uyj3A5JvkKRCRzTwWRbdgDPM8ZKee/qiq6WZM7/XVMcqVg5odcnRT3aThhf2u550UXRrIFSMAlqEg+t2bOv+2iwKYKebSmd00M= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1791890440; c=relaxed/simple; bh=krMSgJEafx6ZZqHtpGf2c6E/T41Ni9GK8F0smEHncFM=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Z6dpb/lJpHVcrU6jmQ2CYSRkpzgKZpEDwOI8Q8UdMXMC1qvrMZTcL7CL3lY4lCErfvP/iUtUVLBRNcxvSOkbd3L37DKbV47iokGez4y41EWnNvKhMV0VF3CTPiiS9CNb5Ad/4ODWD4DJevfaXftNuwe/2DMBF6wdRbfx2vXYzWQ= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=tLJmt12+ DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 25F994BA23FA Received: by mail-pf1-x431.google.com with SMTP id d2e1a72fcca58-88bf90b7f07so791326b3a.3 for ; Tue, 13 Oct 2026 04:20:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1791890439; x=1792495239; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=CZZ46cDtrWZqa/ly1zL3tEffNBfnV89Ze83LQFbRBXY=; b=tLJmt12+ov5qoOUB0l5XHXkTbA/HAKSCQR8Cp4vWG1HXtWZsERHWPGAVkVxePDz0+5 pUEpkbSqjc7dsl8ck1r4QsBoBF/SZ9VQAfZM5bFbu/ovaicI5kuPITse2u3s9Nh/4E1n QNYQYr+XGHCrFnXl/qz3ik4eaXD5PjQjJyqubwMjDnumtxWRqgQLFmlp7QjG78WqcwtW 2yUZKBjLU7cHu2NIvxvZo2p8cgxauurvEe23xEXihYMa4WCqyaZjzdsiF+OSBbiv0UZY rMNbDiuX/gupWGptZF8C5kO9jNzIlyQzNBHntsV6TQ8hQpqe4dPa24Jn0siRSie5eEYG Zniw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1791890439; x=1792495239; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=CZZ46cDtrWZqa/ly1zL3tEffNBfnV89Ze83LQFbRBXY=; b=v8YhzJN0k4/ZPLy04nU2QCN3E6kRF9C4mbb+s4i34EiEbNJkJTDnpTiEU0QEmHu6Sp VQBVSdU/9F3nZ7Cm26n69hYGdqX07gHWmGXPMN4cNgoHdUUJjuEph8D1qjIdHme3js8Q JwKWTnFnrGBMXajtUY+o8AElZDqj+G/P24DeHKtLlRsPpILMV/DeGvs0NfLhTXhq01/c 0KG7kAs+GbXmoljv2t9WdQlSOBbFp+pKC82yPYdiZGWrGqthifK/ycxplEvBc4+qKFsI /QlTts4vTAOnZvPKNuvndrfc27tFCfmluT7tDzvZCjnzct11mGMgqMl0gpNc9IhSsFKe SWWA== X-Gm-Message-State: AFq9FYL/FEtmJeumWfBwDk2Sl0fZlKg1FeOC5x+8tpqmjKfdCyuCnoJL eg2viVwbEkr7p6FOb7cHFzNcIJkCzA5l757vdJ5gBMcN/OPaMr4E/uko X-Gm-Gg: AYBFou3S2ba3n3soQ5ZbIrEJBANQses175DabjSlbGkAVzqDP0SMzwicVg9o/nEK3La gR/ONEePl2o7YwLD3H9NbxluZc4V6TtaIRgC27lYgAMuiPf2HHJKw9fgo8agHUa2/sQDYVYe780 M9p18Vv6ybnewqNfvqV+IrxBGvHy3ok+6eTT9cmidhR8vmxIVVz4nFNgjPKCCWM6uEjQauIFDRP tXMd1xjaRryr/DtAiNOYgSHK9lfEVUZRTlLQKvgVq7D98pJiFrf/5jTueLoKmDlqb+Bg2ZWrtWt cLWukR1jqZ45vzuJ8kYLzQ0howpePyU/nC2q3UjrMmduVIYn2MHL8r80tE6AcNpMcI/GOII7elR +GxRMPsDj1wkYkGKkacUAOvzCz28kbBknV7kM+k6VTFrdMznwrc9ealyIHFoA3JxM6RNe6H+0Ck xJI005TR//ZH4yHNzIM+gBgkRNoB2WAAL/HlpayS9r26m+BCO6xc2UZ1uczC4t6mfTGEaetMSCO psMySrQTXW84kjg7wdeY77XDMe4LZOrFNj9 X-Received: by 2002:a05:6a00:9502:b0:87c:e44e:5694 with SMTP id d2e1a72fcca58-897c6a3440emr11159002b3a.21.1791890439012; Tue, 13 Oct 2026 04:20:39 -0700 (PDT) Received: from dhcp-9-123-1-12.bl1-in.ibm.com ([129.41.58.4]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-896c469ea49sm4858351b3a.53.2026.10.13.04.20.36 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 13 Oct 2026 04:20:38 -0700 (PDT) From: Aditya Vidyadhar Kamath To: ulrich.weigand@de.ibm.com, simon.marchi@polymtl.ca, tom@tromey.com Cc: gdb-patches@sourceware.org, Aditya.Kamath1@ibm.com, sangamesh.swamy@in.ibm.com, Aditya Vidyadhar Kamath Subject: [PATCH v5 4/4] Fix 32-bit GPR and SPR register reads/writes in pthdb callbacks. Date: Tue, 13 Oct 2026 16:49:21 +0530 Message-ID: <20261013111917.68981-8-akamath996@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261013111917.68981-2-akamath996@gmail.com> References: <20261013111917.68981-2-akamath996@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org From: Aditya Vidyadhar Kamath pdc_read_regs reads GPRs into a uint32_t[32] local array when the thread is 32-bit, then copies it into pthdb_context_t::gpr which is uint64_t[32]. The memcpy treated the uint32_t[] data as if it were already uint64_t, so each 32-bit value was silently widened with whatever happened to be adjacent in memory. Replace the memcpy with an element-wise assignment so each value is correctly zero-extended. pdc_read_regs also reads the 32-bit SPR set into a struct ptsprs and copies it with memcpy into &context->msr. The field order in struct ptsprs is iar, msr, cr, lr, ctr, xer, mq, while pthdb_context_t has msr, iar, lr, ctr, cr, xer, fpscr, fpscrx, mq. The memcpy therefore wrote every SPR into the wrong context field and corrupted the fields that follow msr. Replace the memcpy with explicit field-by-field assignments. pdc_write_regs has the symmetric bugs for the write direction. Add the arch64 branch that was missing for GPRs and SPRs: 64-bit: pass context->gpr / &context->msr directly. 32-bit GPRs: truncate each context->gpr[i] (uint64_t) into a local uint32_t gprs32[] array, then call ptrace64aix. 32-bit SPRs: pre-fetch the current struct ptsprs to preserve any reserved fields, fill in the named fields from the context, then call ptrace64aix. Also reword the "untested" comment in both pdc_read_regs and pdc_write_regs. The callbacks do not appear to be invoked during normal debugging, but they must be registered: without them the OS refuses to initialise the pthdb session and several thread-debug functions fail. Note: pt_mq in struct ptsprs is the legacy Multiply/Quotient register from the original POWER architecture. It is present in the struct on all supported AIX releases and is included for completeness. --- gdb/aix-thread.c | 69 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 55 insertions(+), 14 deletions(-) diff --git a/gdb/aix-thread.c b/gdb/aix-thread.c index 795948424b2..f732f5167ff 100644 --- a/gdb/aix-thread.c +++ b/gdb/aix-thread.c @@ -411,11 +411,10 @@ pdc_read_regs (pthdb_user_t user_current_pid, unsigned long long flags, pthdb_context_t *context) { - /* This function doesn't appear to be used, so we could probably - just return 0 here. HOWEVER, if it is not defined, the OS will - complain and several thread debug functions will fail. In case - this is needed, I have implemented what I think it should do, - however this code is untested. */ + /* This function does not appear to be called during normal debugging, + but it must be registered: if the callback is absent the OS refuses + to initialise the pthdb session and several thread-debug functions + fail. */ uint64_t gprs64[ppc_num_gprs]; uint32_t gprs32[ppc_num_gprs]; @@ -443,7 +442,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]; } } @@ -468,7 +468,15 @@ 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->fpscrx = sprs32.pt_fpscrx; + context->mq = sprs32.pt_mq; } } @@ -502,11 +510,13 @@ pdc_write_regs (pthdb_user_t user_current_pid, unsigned long long flags, pthdb_context_t *context) { - /* This function doesn't appear to be used, so we could probably - just return 0 here. HOWEVER, if it is not defined, the OS will - complain and several thread debug functions will fail. In case - this is needed, I have implemented what I think it should do, - however this code is untested. */ + /* This function does not appear to be called during normal debugging, + but it must be registered: if the callback is absent the OS refuses + to initialise the pthdb session and several thread-debug functions + fail. */ + + struct aix_thread_variables *data; + data = get_thread_data_helper_for_pid (user_current_pid); if (debug_aix_thread) gdb_printf (gdb_stdlog, "pdc_write_regs tid=%d flags=%s\n", @@ -514,7 +524,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) @@ -522,7 +542,28 @@ 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; + + /* Pre-fetch so the reserved fields are preserved. */ + if (!ptrace64aix (PTT_READ_SPRS, tid, (long long) &sprs32, 0, NULL)) + memset (&sprs32, 0, sizeof (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_fpscrx = context->fpscrx; + sprs32.pt_mq = context->mq; /* MQ is legacy; present in ptsprs. */ + ptrace64aix (PTT_WRITE_SPRS, tid, (long long) &sprs32, 0, NULL); + } + } /* vector registers. */ if (__power_vmx () && (flags & PTHDB_FLAG_REGS)) -- 2.51.2