public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v2 0/4] Handle null inferiors in target::read_description
@ 2023-07-06 17:23 John Baldwin
  2023-07-06 17:23 ` [PATCH v2 1/4] *-fbsd-nat: Handle null inferior in read_description John Baldwin
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: John Baldwin @ 2023-07-06 17:23 UTC (permalink / raw)
  To: gdb-patches

Change since V1:

- Call the beneath target's read_description method instead of
  returning nullptr.

- ppc-linux-nat now calls the beneath method instead of calling
  ppc_linux_match_description with ppc_linux_no_features.
  ppc_linux_no_features has a wordsize of 0, so
  ppc_linux_match_description would have hit an assertion failure.

- mips-linux-nat is slightly smarter in that it only falls back to
  assuming no DSP if have_dsp < 0.  If have_dsp has previously been
  read and the result cached, then there's no ptrace call to avoid.

- Simplifications to the have_native_target helper function suggested
  by Simon.

John Baldwin (4):
  *-fbsd-nat: Handle null inferior in read_description.
  *-linux-nat: Handle null inferior in read_description.
  Add a have_native_target helper function for use with require.
  Test that native targets can read a tdesc without a process attached.

 gdb/aarch64-fbsd-nat.c                        |  3 +++
 gdb/aarch64-linux-nat.c                       |  3 +++
 gdb/amd64-fbsd-nat.c                          |  3 +++
 gdb/arm-fbsd-nat.c                            |  3 +++
 gdb/arm-linux-nat.c                           |  3 +++
 gdb/i386-fbsd-nat.c                           |  3 +++
 gdb/mips-linux-nat.c                          |  4 +++
 gdb/ppc-linux-nat.c                           |  3 +++
 gdb/riscv-linux-nat.c                         |  3 +++
 gdb/s390-linux-nat.c                          |  3 +++
 .../gdb.base/auto-connect-native-target.exp   | 18 +------------
 .../gdb.base/native-target-noproc-tdesc.exp   | 27 +++++++++++++++++++
 gdb/testsuite/lib/gdb.exp                     | 14 ++++++++++
 gdb/x86-linux-nat.c                           |  3 +++
 14 files changed, 76 insertions(+), 17 deletions(-)
 create mode 100644 gdb/testsuite/gdb.base/native-target-noproc-tdesc.exp

-- 
2.40.0


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

* [PATCH v2 1/4] *-fbsd-nat: Handle null inferior in read_description.
  2023-07-06 17:23 [PATCH v2 0/4] Handle null inferiors in target::read_description John Baldwin
@ 2023-07-06 17:23 ` John Baldwin
  2023-07-06 17:23 ` [PATCH v2 2/4] *-linux-nat: " John Baldwin
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: John Baldwin @ 2023-07-06 17:23 UTC (permalink / raw)
  To: gdb-patches

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..38fb093f139 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 this->beneath ()->read_description ();
+
   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..43e83ff2485 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 this->beneath ()->read_description ();
+
   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..a2255c3c26b 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 this->beneath ()->read_description ();
+
 #ifdef PT_GETXSTATE_INFO
   if (!xsave_probed)
     {
-- 
2.40.0


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

* [PATCH v2 2/4] *-linux-nat: Handle null inferior in read_description.
  2023-07-06 17:23 [PATCH v2 0/4] Handle null inferiors in target::read_description John Baldwin
  2023-07-06 17:23 ` [PATCH v2 1/4] *-fbsd-nat: Handle null inferior in read_description John Baldwin
@ 2023-07-06 17:23 ` John Baldwin
  2023-07-06 17:23 ` [PATCH v2 3/4] Add a have_native_target helper function for use with require John Baldwin
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: John Baldwin @ 2023-07-06 17:23 UTC (permalink / raw)
  To: gdb-patches

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 Linux x86-64:

(gdb) target native
Done.  Use the "run" command to start a process.
(gdb) unset tdesc filename
Couldn't get CS register: No such process.
---
 gdb/aarch64-linux-nat.c | 3 +++
 gdb/arm-linux-nat.c     | 3 +++
 gdb/mips-linux-nat.c    | 4 ++++
 gdb/ppc-linux-nat.c     | 3 +++
 gdb/riscv-linux-nat.c   | 3 +++
 gdb/s390-linux-nat.c    | 3 +++
 gdb/x86-linux-nat.c     | 3 +++
 7 files changed, 22 insertions(+)

diff --git a/gdb/aarch64-linux-nat.c b/gdb/aarch64-linux-nat.c
index ecb2eeb9540..eeb9761bfe5 100644
--- a/gdb/aarch64-linux-nat.c
+++ b/gdb/aarch64-linux-nat.c
@@ -785,6 +785,9 @@ aarch64_linux_nat_target::read_description ()
   gdb_byte regbuf[ARM_VFP3_REGS_SIZE];
   struct iovec iovec;
 
+  if (inferior_ptid == null_ptid)
+    return this->beneath ()->read_description ();
+
   tid = inferior_ptid.pid ();
 
   iovec.iov_base = regbuf;
diff --git a/gdb/arm-linux-nat.c b/gdb/arm-linux-nat.c
index ef3fa008adf..70c6bc684fa 100644
--- a/gdb/arm-linux-nat.c
+++ b/gdb/arm-linux-nat.c
@@ -531,6 +531,9 @@ ps_get_thread_area (struct ps_prochandle *ph,
 const struct target_desc *
 arm_linux_nat_target::read_description ()
 {
+  if (inferior_ptid == null_ptid)
+    return this->beneath ()->read_description ();
+
   CORE_ADDR arm_hwcap = linux_get_hwcap ();
 
   if (have_ptrace_getregset == TRIBOOL_UNKNOWN)
diff --git a/gdb/mips-linux-nat.c b/gdb/mips-linux-nat.c
index 972b5db8e76..8a7cc95f2a4 100644
--- a/gdb/mips-linux-nat.c
+++ b/gdb/mips-linux-nat.c
@@ -458,6 +458,10 @@ mips_linux_nat_target::read_description ()
 
   if (have_dsp < 0)
     {
+      /* Assume no DSP if there is no inferior to inspect with ptrace.  */
+      if (inferior_ptid == null_ptid)
+	return _MIPS_SIM == _ABIO32 ? tdesc_mips_linux : tdesc_mips64_linux;
+
       int tid = get_ptrace_pid (inferior_ptid);
 
       errno = 0;
diff --git a/gdb/ppc-linux-nat.c b/gdb/ppc-linux-nat.c
index 55dcda9f525..d14aba694e5 100644
--- a/gdb/ppc-linux-nat.c
+++ b/gdb/ppc-linux-nat.c
@@ -1941,6 +1941,9 @@ ppc_linux_nat_target::auxv_parse (const gdb_byte **readptr,
 const struct target_desc *
 ppc_linux_nat_target::read_description ()
 {
+  if (inferior_ptid == null_ptid)
+    return this->beneath ()->read_description ();
+
   int tid = inferior_ptid.pid ();
 
   if (have_ptrace_getsetevrregs)
diff --git a/gdb/riscv-linux-nat.c b/gdb/riscv-linux-nat.c
index 8be4a5ac3e5..9492cb68079 100644
--- a/gdb/riscv-linux-nat.c
+++ b/gdb/riscv-linux-nat.c
@@ -201,6 +201,9 @@ fill_fpregset (const struct regcache *regcache, prfpregset_t *fpregs,
 const struct target_desc *
 riscv_linux_nat_target::read_description ()
 {
+  if (inferior_ptid == null_ptid)
+    return this->beneath ()->read_description ();
+
   const struct riscv_gdbarch_features features
     = riscv_linux_read_features (inferior_ptid.pid ());
   return riscv_lookup_target_description (features);
diff --git a/gdb/s390-linux-nat.c b/gdb/s390-linux-nat.c
index fc3917d30be..8f54e9f6322 100644
--- a/gdb/s390-linux-nat.c
+++ b/gdb/s390-linux-nat.c
@@ -987,6 +987,9 @@ s390_linux_nat_target::auxv_parse (const gdb_byte **readptr,
 const struct target_desc *
 s390_linux_nat_target::read_description ()
 {
+  if (inferior_ptid == null_ptid)
+    return this->beneath ()->read_description ();
+
   int tid = inferior_ptid.pid ();
 
   have_regset_last_break
diff --git a/gdb/x86-linux-nat.c b/gdb/x86-linux-nat.c
index fd2145244cc..ca4eaf5b645 100644
--- a/gdb/x86-linux-nat.c
+++ b/gdb/x86-linux-nat.c
@@ -115,6 +115,9 @@ x86_linux_nat_target::read_description ()
   static uint64_t xcr0;
   uint64_t xcr0_features_bits;
 
+  if (inferior_ptid == null_ptid)
+    return this->beneath ()->read_description ();
+
   tid = inferior_ptid.pid ();
 
 #ifdef __x86_64__
-- 
2.40.0


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

* [PATCH v2 3/4] Add a have_native_target helper function for use with require.
  2023-07-06 17:23 [PATCH v2 0/4] Handle null inferiors in target::read_description John Baldwin
  2023-07-06 17:23 ` [PATCH v2 1/4] *-fbsd-nat: Handle null inferior in read_description John Baldwin
  2023-07-06 17:23 ` [PATCH v2 2/4] *-linux-nat: " John Baldwin
@ 2023-07-06 17:23 ` John Baldwin
  2023-07-06 17:23 ` [PATCH v2 4/4] Test that native targets can read a tdesc without a process attached John Baldwin
  2023-07-07 16:15 ` [PATCH v2 0/4] Handle null inferiors in target::read_description Tom Tromey
  4 siblings, 0 replies; 6+ messages in thread
From: John Baldwin @ 2023-07-06 17:23 UTC (permalink / raw)
  To: gdb-patches

Move logic from auto-connect-native-target.exp into this helper.
---
 .../gdb.base/auto-connect-native-target.exp    | 18 +-----------------
 gdb/testsuite/lib/gdb.exp                      | 14 ++++++++++++++
 2 files changed, 15 insertions(+), 17 deletions(-)

diff --git a/gdb/testsuite/gdb.base/auto-connect-native-target.exp b/gdb/testsuite/gdb.base/auto-connect-native-target.exp
index 002a6d61126..0586cd4baf4 100644
--- a/gdb/testsuite/gdb.base/auto-connect-native-target.exp
+++ b/gdb/testsuite/gdb.base/auto-connect-native-target.exp
@@ -22,23 +22,7 @@ if {[prepare_for_testing "failed to prepare" $testfile $srcfile debug]} {
     return -1
 }
 
-# Whether this GDB is configured with a "native" target.
-set have_native 0
-
-set test "help target native"
-gdb_test_multiple $test $test {
-    -re "Undefined target command.*$gdb_prompt $" {
-	set have_native 0
-    }
-    -re "Native process.*$gdb_prompt $" {
-	set have_native 1
-    }
-}
-
-if { !$have_native } {
-    unsupported "no \"target native\" support."
-    return
-}
+require have_native_target
 
 # Returns the topmost target pushed on the target stack.  TEST is used
 # as test message.
diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp
index b4900ae25a6..0a7c593672a 100644
--- a/gdb/testsuite/lib/gdb.exp
+++ b/gdb/testsuite/lib/gdb.exp
@@ -9807,6 +9807,20 @@ gdb_caching_proc have_compile_and_link_flag { flag } {
 		additional_flags=$flag]
 }
 
+# Return 1 if this GDB is configured with a "native" target.
+
+gdb_caching_proc have_native_target {} {
+    gdb_test_multiple "help target native" "" {
+	-re -wrap "Undefined target command.*" {
+	    return 0
+	}
+	-re -wrap "Native process.*" {
+	    return 1
+	}
+    }
+    return 0
+}
+
 # Handle include file $srcdir/$subdir/FILE.
 
 proc include_file { file } {
-- 
2.40.0


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

* [PATCH v2 4/4] Test that native targets can read a tdesc without a process attached.
  2023-07-06 17:23 [PATCH v2 0/4] Handle null inferiors in target::read_description John Baldwin
                   ` (2 preceding siblings ...)
  2023-07-06 17:23 ` [PATCH v2 3/4] Add a have_native_target helper function for use with require John Baldwin
@ 2023-07-06 17:23 ` John Baldwin
  2023-07-07 16:15 ` [PATCH v2 0/4] Handle null inferiors in target::read_description Tom Tromey
  4 siblings, 0 replies; 6+ messages in thread
From: John Baldwin @ 2023-07-06 17:23 UTC (permalink / raw)
  To: gdb-patches

This ensures that 'unset tdesc filename' does not generate any output
on a "bare" native target inferior without an attached process.
---
 .../gdb.base/native-target-noproc-tdesc.exp   | 27 +++++++++++++++++++
 1 file changed, 27 insertions(+)
 create mode 100644 gdb/testsuite/gdb.base/native-target-noproc-tdesc.exp

diff --git a/gdb/testsuite/gdb.base/native-target-noproc-tdesc.exp b/gdb/testsuite/gdb.base/native-target-noproc-tdesc.exp
new file mode 100644
index 00000000000..820d9941c71
--- /dev/null
+++ b/gdb/testsuite/gdb.base/native-target-noproc-tdesc.exp
@@ -0,0 +1,27 @@
+# Copyright 2023 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Test "unset tdesc filename" without an attached process on native
+# targets.
+
+clean_restart
+
+require have_native_target
+
+# Manually attach the native target
+gdb_test "target native" "Done.  Use the \"run\" command to start a process."
+
+# Load a default target description
+gdb_test_no_output "unset tdesc filename"
-- 
2.40.0


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

* Re: [PATCH v2 0/4] Handle null inferiors in target::read_description
  2023-07-06 17:23 [PATCH v2 0/4] Handle null inferiors in target::read_description John Baldwin
                   ` (3 preceding siblings ...)
  2023-07-06 17:23 ` [PATCH v2 4/4] Test that native targets can read a tdesc without a process attached John Baldwin
@ 2023-07-07 16:15 ` Tom Tromey
  4 siblings, 0 replies; 6+ messages in thread
From: Tom Tromey @ 2023-07-07 16:15 UTC (permalink / raw)
  To: John Baldwin; +Cc: gdb-patches

>>>>> "John" == John Baldwin <jhb@FreeBSD.org> writes:

John> Change since V1:
John> - Call the beneath target's read_description method instead of
John>   returning nullptr.

FWIW, I read through these.  I didn't see any red flags, but then again,
I'm not sure I really understand the underlying scenario very well.

I didn't want this to go un-answered, though.  IMO it is ok.

Tom

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

end of thread, other threads:[~2023-07-07 16:15 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-06 17:23 [PATCH v2 0/4] Handle null inferiors in target::read_description John Baldwin
2023-07-06 17:23 ` [PATCH v2 1/4] *-fbsd-nat: Handle null inferior in read_description John Baldwin
2023-07-06 17:23 ` [PATCH v2 2/4] *-linux-nat: " John Baldwin
2023-07-06 17:23 ` [PATCH v2 3/4] Add a have_native_target helper function for use with require John Baldwin
2023-07-06 17:23 ` [PATCH v2 4/4] Test that native targets can read a tdesc without a process attached John Baldwin
2023-07-07 16:15 ` [PATCH v2 0/4] Handle null inferiors in target::read_description Tom Tromey

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).