[4/5] gdb: add inferior parameter to target_find_description

Message ID 20221124160428.83804-5-simon.marchi@efficios.com
State New
Headers
Series Make some functions independent of current inferior |

Commit Message

Simon Marchi Nov. 24, 2022, 4:04 p.m. UTC
  From: Simon Marchi <simon.marchi@polymtl.ca>

Make target_find_description not dependent on the current inferior on
entry.  Add an inferior parameter, and make it switch the current
inferior temporarily where needed.

Make callers pass the current inferior, no change in behavior is
expected.

Change-Id: I6fbe03a91e812c003b7946ba7ccc807a8b7369e6
---
 gdb/infcmd.c              |  2 +-
 gdb/infrun.c              |  2 +-
 gdb/remote.c              |  6 +++---
 gdb/target-descriptions.c | 31 ++++++++++++++++++-------------
 gdb/target-descriptions.h |  6 +++---
 gdb/tracefile-tfile.c     |  2 +-
 6 files changed, 27 insertions(+), 22 deletions(-)
  

Comments

Simon Marchi Nov. 24, 2022, 4:25 p.m. UTC | #1
> @@ -546,16 +545,22 @@ target_find_description (void)
>    if (!tdesc_info->filename.empty ())
>      tdesc_info->tdesc = file_read_description_xml (tdesc_info->filename.data ());
>  
> +  /* The calls that get the target description from the target depend on INF
> +     being the current inferior, and some targets need a specific thread to
> +     be selected.  */
> +  scoped_restore_current_thread restore_thread;
> +  thread_info *thread = any_thread_of_inferior (inf);
> +  gdb_assert (thread != nullptr);
> +  switch_to_thread (thread);
Sorry, looks like I messed up my testing, this doesn't work when using
the "set tdesc filename" command while there are no threads:


 37 (gdb) set tdesc filename /home/smarchi/build/binutils-gdb/gdb/testsuite/outputs/gdb.arch/arc-tdesc-cpu/trivial.xml^M
 38 /home/smarchi/src/binutils-gdb/gdb/thread.c:631: internal-error: any_thread_of_inferior: Assertion `inf->pid != 0' failed.^M
 39 A problem internal to GDB has been detected,^M
 40 further debugging may prove unreliable.^M
 41 ----- Backtrace -----^M
 42 FAIL: gdb.arch/arc-tdesc-cpu.exp: set tdesc filename /home/smarchi/build/binutils-gdb/gdb/testsuite/outputs/gdb.arch/arc-tdesc-cpu/trivial.xml (GDB internal error)

I made it this way because the remote target uses inferior_ptid to set
the general thread in remote_target::xfer_partial, regardless of the
object.  For TARGET_OBJECT_AVAILABLE_FEATURES, since target descriptions
are a per-inferior thing (for now), it could look at the current
inferior instead, it doesn't really need a current thread.  This is part
of my larger work that I picked these patches from, so I will have to
re-check the order of the changes.

Simon
  

Patch

diff --git a/gdb/infcmd.c b/gdb/infcmd.c
index f7bce0d0399..f281914ce54 100644
--- a/gdb/infcmd.c
+++ b/gdb/infcmd.c
@@ -251,7 +251,7 @@  post_create_inferior (int from_tty)
      Targets which need to access registers during to_open,
      to_create_inferior, or to_attach should do it earlier; but many
      don't need to.  */
-  target_find_description ();
+  target_find_description (current_inferior ());
 
   /* Now that we know the register layout, retrieve current PC.  But
      if the PC is unavailable (e.g., we're opening a core file with
diff --git a/gdb/infrun.c b/gdb/infrun.c
index 96346e1f25b..c678d5accce 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -1272,7 +1272,7 @@  follow_exec (ptid_t ptid, const char *exec_file_target)
      architecture, and the old executable may e.g., be 32-bit, while
      the new one 64-bit), and before anything involving memory or
      registers.  */
-  target_find_description ();
+  target_find_description (inf);
 
   gdb::observers::inferior_execd.notify (inf);
 
diff --git a/gdb/remote.c b/gdb/remote.c
index 5118ecd0a31..ca6fd535a54 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -4826,7 +4826,7 @@  remote_target::start_remote_1 (int from_tty, int extended_p)
 
   /* Next, if the target can specify a description, read it.  We do
      this before anything involving memory or registers.  */
-  target_find_description ();
+  target_find_description (current_inferior ());
 
   /* Next, now that we know something about the target, update the
      address spaces in the program spaces.  */
@@ -4963,7 +4963,7 @@  remote_target::start_remote_1 (int from_tty, int extended_p)
 	  && gdbarch_target_desc (target_gdbarch ()) == NULL)
 	{
 	  target_clear_description ();
-	  target_find_description ();
+	  target_find_description (current_inferior ());
 	}
 
       /* Use the previously fetched status.  */
@@ -6187,7 +6187,7 @@  extended_remote_target::attach (const char *args, int from_tty)
 
   /* Next, if the target can specify a description, read it.  We do
      this before anything involving memory or registers.  */
-  target_find_description ();
+  target_find_description (current_inferior ());
 
   if (!target_is_non_stop_p ())
     {
diff --git a/gdb/target-descriptions.c b/gdb/target-descriptions.c
index 57d23747f26..40c04e0770f 100644
--- a/gdb/target-descriptions.c
+++ b/gdb/target-descriptions.c
@@ -520,13 +520,12 @@  target_desc_info_free (struct target_desc_info *tdesc_info)
 
 static std::string tdesc_filename_cmd_string;
 
-/* Fetch the current target's description, and switch the current
-   architecture to one which incorporates that description.  */
+/* See target-descriptions.h.  */
 
 void
-target_find_description (void)
+target_find_description (inferior *inf)
 {
-  target_desc_info *tdesc_info = get_tdesc_info (current_inferior ());
+  target_desc_info *tdesc_info = get_tdesc_info (inf);
 
   /* If we've already fetched a description from the target, don't do
      it again.  This allows a target to fetch the description early,
@@ -538,7 +537,7 @@  target_find_description (void)
   /* The current architecture should not have any target description
      specified.  It should have been cleared, e.g. when we
      disconnected from the previous target.  */
-  gdb_assert (gdbarch_target_desc (target_gdbarch ()) == NULL);
+  gdb_assert (gdbarch_target_desc (inf->gdbarch) == nullptr);
 
   /* First try to fetch an XML description from the user-specified
      file.  */
@@ -546,16 +545,22 @@  target_find_description (void)
   if (!tdesc_info->filename.empty ())
     tdesc_info->tdesc = file_read_description_xml (tdesc_info->filename.data ());
 
+  /* The calls that get the target description from the target depend on INF
+     being the current inferior, and some targets need a specific thread to
+     be selected.  */
+  scoped_restore_current_thread restore_thread;
+  thread_info *thread = any_thread_of_inferior (inf);
+  gdb_assert (thread != nullptr);
+  switch_to_thread (thread);
+
   /* Next try to read the description from the current target using
      target objects.  */
   if (tdesc_info->tdesc == nullptr)
-    tdesc_info->tdesc = target_read_description_xml
-      (current_inferior ()->top_target ());
+    tdesc_info->tdesc = target_read_description_xml (inf->top_target ());
 
   /* If that failed try a target-specific hook.  */
   if (tdesc_info->tdesc == nullptr)
-    tdesc_info->tdesc = target_read_description
-      (current_inferior ()->top_target ());
+    tdesc_info->tdesc = target_read_description (inf->top_target ());
 
   /* If a non-NULL description was returned, then update the current
      architecture.  */
@@ -564,13 +569,13 @@  target_find_description (void)
       struct gdbarch_info info;
 
       info.target_desc = tdesc_info->tdesc;
-      if (!gdbarch_update_p (current_inferior (), info))
+      if (!gdbarch_update_p (inf, info))
 	warning (_("Architecture rejected target-supplied description"));
       else
 	{
 	  struct tdesc_arch_data *data;
 
-	  data = get_arch_data (target_gdbarch ());
+	  data = get_arch_data (inf->gdbarch);
 	  if (tdesc_has_registers (tdesc_info->tdesc)
 	      && data->arch_regs.empty ())
 	    warning (_("Target-supplied registers are not supported "
@@ -1289,7 +1294,7 @@  set_tdesc_filename_cmd (const char *args, int from_tty,
   tdesc_info->filename = tdesc_filename_cmd_string;
 
   target_clear_description ();
-  target_find_description ();
+  target_find_description (current_inferior ());
 }
 
 static void
@@ -1316,7 +1321,7 @@  unset_tdesc_filename_cmd (const char *args, int from_tty)
 
   tdesc_info->filename.clear ();
   target_clear_description ();
-  target_find_description ();
+  target_find_description (current_inferior ());
 }
 
 /* Print target description in C.  */
diff --git a/gdb/target-descriptions.h b/gdb/target-descriptions.h
index 3049b783e2f..ab534488d65 100644
--- a/gdb/target-descriptions.h
+++ b/gdb/target-descriptions.h
@@ -31,10 +31,10 @@  struct target_ops;
 struct target_desc_info;
 struct inferior;
 
-/* Fetch the current inferior's description, and switch its current
-   architecture to one which incorporates that description.  */
+/* Fetch INFERIOR's target description, and switch its architecture to one which
+   incorporates that description.  */
 
-void target_find_description (void);
+void target_find_description (inferior *inf);
 
 /* Discard any description fetched from the target for the current
    inferior, and switch the current architecture to one with no target
diff --git a/gdb/tracefile-tfile.c b/gdb/tracefile-tfile.c
index 3266f357a27..b8302902af4 100644
--- a/gdb/tracefile-tfile.c
+++ b/gdb/tracefile-tfile.c
@@ -538,7 +538,7 @@  tfile_target_open (const char *arg, int from_tty)
 	}
 
       /* By now, tdesc lines have been read from tfile - let's parse them.  */
-      target_find_description ();
+      target_find_description (current_inferior ());
 
       /* Record the starting offset of the binary trace data.  */
       trace_frames_offset = bytes;