public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] [aarch64] tpidr2: Fix erroneous detection logic for TPIDR2
@ 2023-03-14 14:22 Luis Machado
  2023-04-03  9:12 ` Luis Machado
  0 siblings, 1 reply; 2+ messages in thread
From: Luis Machado @ 2023-03-14 14:22 UTC (permalink / raw)
  To: gdb-patches

The detection logic for TPIDR2 was implemented incorrectly.  Originally
the detection was supposed to be through a ptrace error code, but in reality,
for backwards compatibility, the detection should be based on the size of
the returned iovec.

For instance, if a target supports both TPIDR and TPIDR2, ptrace will return a
iovec size of 16.  If a target only supports TPIDR and not TPIDR2, it will
return a iovec size of 8, even if we asked for 16 bytes.

This patch fixes this issue in code that is shared between gdb and gdbserver,
therefore both gdb and gdbserver are fixed.

Tested on AArch64/Linux Ubuntu 20.04.
---
 gdb/nat/aarch64-linux.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/gdb/nat/aarch64-linux.c b/gdb/nat/aarch64-linux.c
index 6c8a78f1729..a84faf1cfbf 100644
--- a/gdb/nat/aarch64-linux.c
+++ b/gdb/nat/aarch64-linux.c
@@ -261,13 +261,13 @@ aarch64_tls_register_count (int tid)
   iovec.iov_base = tls_regs;
   iovec.iov_len = sizeof (tls_regs);
 
-  /* Attempt to read both TPIDR and TPIDR2.  If the request fails, it means
-     the Linux Kernel does not support TPIDR2.
-
-     Otherwise the Linux Kernel supports both TPIDR and TPIDR2.  */
+  /* Attempt to read both TPIDR and TPIDR2.  If ptrace returns less data than
+     we are expecting, that means it doesn't support all the registers.  From
+     the iovec length, figure out how many TPIDR registers the target actually
+     supports.  */
   if (ptrace (PTRACE_GETREGSET, tid, NT_ARM_TLS, &iovec) != 0)
-    return 1;
+    return 0;
 
-  /* TPIDR2 is available as well.  */
-  return 2;
+  /* Calculate how many TPIDR registers we have.  */
+  return iovec.iov_len / sizeof (uint64_t);
 }
-- 
2.25.1


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

* Re: [PATCH] [aarch64] tpidr2: Fix erroneous detection logic for TPIDR2
  2023-03-14 14:22 [PATCH] [aarch64] tpidr2: Fix erroneous detection logic for TPIDR2 Luis Machado
@ 2023-04-03  9:12 ` Luis Machado
  0 siblings, 0 replies; 2+ messages in thread
From: Luis Machado @ 2023-04-03  9:12 UTC (permalink / raw)
  To: gdb-patches

On 3/14/23 14:22, Luis Machado via Gdb-patches wrote:
> The detection logic for TPIDR2 was implemented incorrectly.  Originally
> the detection was supposed to be through a ptrace error code, but in reality,
> for backwards compatibility, the detection should be based on the size of
> the returned iovec.
> 
> For instance, if a target supports both TPIDR and TPIDR2, ptrace will return a
> iovec size of 16.  If a target only supports TPIDR and not TPIDR2, it will
> return a iovec size of 8, even if we asked for 16 bytes.
> 
> This patch fixes this issue in code that is shared between gdb and gdbserver,
> therefore both gdb and gdbserver are fixed.
> 
> Tested on AArch64/Linux Ubuntu 20.04.
> ---
>   gdb/nat/aarch64-linux.c | 14 +++++++-------
>   1 file changed, 7 insertions(+), 7 deletions(-)
> 
> diff --git a/gdb/nat/aarch64-linux.c b/gdb/nat/aarch64-linux.c
> index 6c8a78f1729..a84faf1cfbf 100644
> --- a/gdb/nat/aarch64-linux.c
> +++ b/gdb/nat/aarch64-linux.c
> @@ -261,13 +261,13 @@ aarch64_tls_register_count (int tid)
>     iovec.iov_base = tls_regs;
>     iovec.iov_len = sizeof (tls_regs);
>   
> -  /* Attempt to read both TPIDR and TPIDR2.  If the request fails, it means
> -     the Linux Kernel does not support TPIDR2.
> -
> -     Otherwise the Linux Kernel supports both TPIDR and TPIDR2.  */
> +  /* Attempt to read both TPIDR and TPIDR2.  If ptrace returns less data than
> +     we are expecting, that means it doesn't support all the registers.  From
> +     the iovec length, figure out how many TPIDR registers the target actually
> +     supports.  */
>     if (ptrace (PTRACE_GETREGSET, tid, NT_ARM_TLS, &iovec) != 0)
> -    return 1;
> +    return 0;
>   
> -  /* TPIDR2 is available as well.  */
> -  return 2;
> +  /* Calculate how many TPIDR registers we have.  */
> +  return iovec.iov_len / sizeof (uint64_t);
>   }

Pushed now as d747348d02f12121a2e897f7789ff27ffbd2f23d.

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

end of thread, other threads:[~2023-04-03  9:13 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-14 14:22 [PATCH] [aarch64] tpidr2: Fix erroneous detection logic for TPIDR2 Luis Machado
2023-04-03  9:12 ` Luis Machado

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).