[1/4] *-fbsd-nat: Handle null inferior in read_description.

Message ID 20230526175742.66885-2-jhb@FreeBSD.org
State New
Headers
Series Handle null inferiors in target::read_description |

Commit Message

John Baldwin May 26, 2023, 5:57 p.m. UTC
  Don't invoke ptrace in the target read_description method if there is
not an active inferior to query via ptrace.  Instead, use the default
register set for the architecture.

Previously the native target could report an error from a failed
ptrace operation when fetching a tdesc without an attached process.
For example on FreeBSD/amd64:

(gdb) target native
Done.  Use the "run" command to start a process.
(gdb) unset tdesc filename
Couldn't get registers: Operation not permitted.
---
 gdb/aarch64-fbsd-nat.c | 3 +++
 gdb/amd64-fbsd-nat.c   | 3 +++
 gdb/arm-fbsd-nat.c     | 3 +++
 gdb/i386-fbsd-nat.c    | 3 +++
 4 files changed, 12 insertions(+)
  

Comments

Simon Marchi July 6, 2023, 4:18 p.m. UTC | #1
On 5/26/23 13:57, John Baldwin wrote:
> Don't invoke ptrace in the target read_description method if there is
> not an active inferior to query via ptrace.  Instead, use the default
> register set for the architecture.
> 
> Previously the native target could report an error from a failed
> ptrace operation when fetching a tdesc without an attached process.
> For example on FreeBSD/amd64:
> 
> (gdb) target native
> Done.  Use the "run" command to start a process.
> (gdb) unset tdesc filename
> Couldn't get registers: Operation not permitted.
> ---
>  gdb/aarch64-fbsd-nat.c | 3 +++
>  gdb/amd64-fbsd-nat.c   | 3 +++
>  gdb/arm-fbsd-nat.c     | 3 +++
>  gdb/i386-fbsd-nat.c    | 3 +++
>  4 files changed, 12 insertions(+)
> 
> diff --git a/gdb/aarch64-fbsd-nat.c b/gdb/aarch64-fbsd-nat.c
> index 709f5162ce0..29b58e5ff4a 100644
> --- a/gdb/aarch64-fbsd-nat.c
> +++ b/gdb/aarch64-fbsd-nat.c
> @@ -120,6 +120,9 @@ aarch64_fbsd_nat_target::store_registers (struct regcache *regcache,
>  const struct target_desc *
>  aarch64_fbsd_nat_target::read_description ()
>  {
> +  if (inferior_ptid == null_ptid)
> +    return nullptr;
> +
>    aarch64_features features;
>    features.tls = have_regset (inferior_ptid, NT_ARM_TLS)? 1 : 0;
>    return aarch64_read_description (features);
> diff --git a/gdb/amd64-fbsd-nat.c b/gdb/amd64-fbsd-nat.c
> index bae267f230f..adbd85571a5 100644
> --- a/gdb/amd64-fbsd-nat.c
> +++ b/gdb/amd64-fbsd-nat.c
> @@ -310,6 +310,9 @@ amd64_fbsd_nat_target::read_description ()
>    struct reg regs;
>    int is64;
>  
> +  if (inferior_ptid == null_ptid)
> +    return nullptr;

Why do you return nullptr here, instead of calling the beneath target,
as done in arm-fbsd-nat.c?  Calling the beneath target seems like the
right thing to do.  In practice, it will likely reach
dummy_target::read_description, which returns nullptr.

Simon
  
John Baldwin July 6, 2023, 4:56 p.m. UTC | #2
On 7/6/23 9:18 AM, Simon Marchi wrote:
> On 5/26/23 13:57, John Baldwin wrote:
>> Don't invoke ptrace in the target read_description method if there is
>> not an active inferior to query via ptrace.  Instead, use the default
>> register set for the architecture.
>>
>> Previously the native target could report an error from a failed
>> ptrace operation when fetching a tdesc without an attached process.
>> For example on FreeBSD/amd64:
>>
>> (gdb) target native
>> Done.  Use the "run" command to start a process.
>> (gdb) unset tdesc filename
>> Couldn't get registers: Operation not permitted.
>> ---
>>   gdb/aarch64-fbsd-nat.c | 3 +++
>>   gdb/amd64-fbsd-nat.c   | 3 +++
>>   gdb/arm-fbsd-nat.c     | 3 +++
>>   gdb/i386-fbsd-nat.c    | 3 +++
>>   4 files changed, 12 insertions(+)
>>
>> diff --git a/gdb/aarch64-fbsd-nat.c b/gdb/aarch64-fbsd-nat.c
>> index 709f5162ce0..29b58e5ff4a 100644
>> --- a/gdb/aarch64-fbsd-nat.c
>> +++ b/gdb/aarch64-fbsd-nat.c
>> @@ -120,6 +120,9 @@ aarch64_fbsd_nat_target::store_registers (struct regcache *regcache,
>>   const struct target_desc *
>>   aarch64_fbsd_nat_target::read_description ()
>>   {
>> +  if (inferior_ptid == null_ptid)
>> +    return nullptr;
>> +
>>     aarch64_features features;
>>     features.tls = have_regset (inferior_ptid, NT_ARM_TLS)? 1 : 0;
>>     return aarch64_read_description (features);
>> diff --git a/gdb/amd64-fbsd-nat.c b/gdb/amd64-fbsd-nat.c
>> index bae267f230f..adbd85571a5 100644
>> --- a/gdb/amd64-fbsd-nat.c
>> +++ b/gdb/amd64-fbsd-nat.c
>> @@ -310,6 +310,9 @@ amd64_fbsd_nat_target::read_description ()
>>     struct reg regs;
>>     int is64;
>>   
>> +  if (inferior_ptid == null_ptid)
>> +    return nullptr;
> 
> Why do you return nullptr here, instead of calling the beneath target,
> as done in arm-fbsd-nat.c?  Calling the beneath target seems like the
> right thing to do.  In practice, it will likely reach
> dummy_target::read_description, which returns nullptr.

I think you are right, and I will change both this and patch 2 for the
Linux targets to call the beneath one.  My thinking was that in general
returning nullptr causes the caller code to pick a "default" tdesc, but
that for arm-fbsd-nat.c I was trying to match the existing code
(copied from arm-linux-nat.c) which calls the beneath target for its fallback
instead of returning nullptr.
  

Patch

diff --git a/gdb/aarch64-fbsd-nat.c b/gdb/aarch64-fbsd-nat.c
index 709f5162ce0..29b58e5ff4a 100644
--- a/gdb/aarch64-fbsd-nat.c
+++ b/gdb/aarch64-fbsd-nat.c
@@ -120,6 +120,9 @@  aarch64_fbsd_nat_target::store_registers (struct regcache *regcache,
 const struct target_desc *
 aarch64_fbsd_nat_target::read_description ()
 {
+  if (inferior_ptid == null_ptid)
+    return nullptr;
+
   aarch64_features features;
   features.tls = have_regset (inferior_ptid, NT_ARM_TLS)? 1 : 0;
   return aarch64_read_description (features);
diff --git a/gdb/amd64-fbsd-nat.c b/gdb/amd64-fbsd-nat.c
index bae267f230f..adbd85571a5 100644
--- a/gdb/amd64-fbsd-nat.c
+++ b/gdb/amd64-fbsd-nat.c
@@ -310,6 +310,9 @@  amd64_fbsd_nat_target::read_description ()
   struct reg regs;
   int is64;
 
+  if (inferior_ptid == null_ptid)
+    return nullptr;
+
   if (ptrace (PT_GETREGS, inferior_ptid.pid (),
 	      (PTRACE_TYPE_ARG3) &regs, 0) == -1)
     perror_with_name (_("Couldn't get registers"));
diff --git a/gdb/arm-fbsd-nat.c b/gdb/arm-fbsd-nat.c
index 5181281b27d..cf22fc6cce9 100644
--- a/gdb/arm-fbsd-nat.c
+++ b/gdb/arm-fbsd-nat.c
@@ -93,6 +93,9 @@  arm_fbsd_nat_target::read_description ()
   const struct target_desc *desc;
   bool tls = false;
 
+  if (inferior_ptid == null_ptid)
+    return this->beneath ()->read_description ();
+
 #ifdef PT_GETREGSET
   tls = have_regset (inferior_ptid, NT_ARM_TLS) != 0;
 #endif
diff --git a/gdb/i386-fbsd-nat.c b/gdb/i386-fbsd-nat.c
index 927771e8b20..26ae420949e 100644
--- a/gdb/i386-fbsd-nat.c
+++ b/gdb/i386-fbsd-nat.c
@@ -315,6 +315,9 @@  i386_fbsd_nat_target::read_description ()
 #endif
   static int xmm_probed;
 
+  if (inferior_ptid == null_ptid)
+    return nullptr;
+
 #ifdef PT_GETXSTATE_INFO
   if (!xsave_probed)
     {