[3/5] gdb: add inferior parameter to gdbarch_update_p

Message ID 20221124160428.83804-4-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>

gdbarch_update_p sets the current inferior's gdbarch, based on the
passed gdbarch_info.  Add an inferior parameter, so it doesn't depend on
the current inferior.

The difficult thing is to ensure that all gdbarch and osabi init
functions are independent of the current inferior.  There are so many,
it's not realistic to read them all to be sure.  However, I added
temporarily a little hack to ensure current_inferior isn't called during
`rego->init`, and ran "maint selftest" on an all targets build, and it
worked fine.  Given that this instantiates many variants of all
architectures, it gives some confidence that no init function relies on
the current inferior.

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

Change-Id: I011e40e74a0e4e2dd5eeb0b3b59eb39f4813ab9e
---
 gdb/arch-utils.c          | 27 ++++++++++++++-------------
 gdb/arm-tdep.c            |  2 +-
 gdb/cris-tdep.c           |  6 +++---
 gdb/gdbarch.h             |  4 ++--
 gdb/i386-darwin-nat.c     |  2 +-
 gdb/mips-tdep.c           | 10 +++++-----
 gdb/osabi.c               |  3 ++-
 gdb/rs6000-aix-nat.c      |  2 +-
 gdb/rs6000-tdep.c         |  4 ++--
 gdb/target-descriptions.c |  4 ++--
 10 files changed, 33 insertions(+), 31 deletions(-)
  

Patch

diff --git a/gdb/arch-utils.c b/gdb/arch-utils.c
index 92caa5c3c4a..878c1849e8a 100644
--- a/gdb/arch-utils.c
+++ b/gdb/arch-utils.c
@@ -396,13 +396,13 @@  set_endian (const char *ignore_args, int from_tty, struct cmd_list_element *c)
   if (set_endian_string == endian_auto)
     {
       target_byte_order_user = BFD_ENDIAN_UNKNOWN;
-      if (! gdbarch_update_p (info))
+      if (! gdbarch_update_p (current_inferior (), info))
 	internal_error (_("set_endian: architecture update failed"));
     }
   else if (set_endian_string == endian_little)
     {
       info.byte_order = BFD_ENDIAN_LITTLE;
-      if (! gdbarch_update_p (info))
+      if (! gdbarch_update_p (current_inferior (), info))
 	gdb_printf (gdb_stderr,
 		    _("Little endian target not supported by GDB\n"));
       else
@@ -411,7 +411,7 @@  set_endian (const char *ignore_args, int from_tty, struct cmd_list_element *c)
   else if (set_endian_string == endian_big)
     {
       info.byte_order = BFD_ENDIAN_BIG;
-      if (! gdbarch_update_p (info))
+      if (! gdbarch_update_p (current_inferior (), info))
 	gdb_printf (gdb_stderr,
 		    _("Big endian target not supported by GDB\n"));
       else
@@ -553,7 +553,7 @@  set_architecture (const char *ignore_args,
   if (strcmp (set_architecture_string, "auto") == 0)
     {
       target_architecture_user = NULL;
-      if (!gdbarch_update_p (info))
+      if (!gdbarch_update_p (current_inferior (), info))
 	internal_error (_("could not select an architecture automatically"));
     }
   else
@@ -561,7 +561,7 @@  set_architecture (const char *ignore_args,
       info.bfd_arch_info = bfd_scan_arch (set_architecture_string);
       if (info.bfd_arch_info == NULL)
 	internal_error (_("set_architecture: bfd_scan_arch failed"));
-      if (gdbarch_update_p (info))
+      if (gdbarch_update_p (current_inferior (), info))
 	target_architecture_user = info.bfd_arch_info;
       else
 	gdb_printf (gdb_stderr,
@@ -571,22 +571,23 @@  set_architecture (const char *ignore_args,
   show_architecture (gdb_stdout, from_tty, NULL, NULL);
 }
 
-/* Try to select a global architecture that matches "info".  Return
-   non-zero if the attempt succeeds.  */
+/* See gdbarch.h.  */
+
 int
-gdbarch_update_p (struct gdbarch_info info)
+gdbarch_update_p (inferior *inf, struct gdbarch_info info)
 {
   struct gdbarch *new_gdbarch;
 
   /* Check for the current file.  */
   if (info.abfd == NULL)
-    info.abfd = current_program_space->exec_bfd ();
+    info.abfd = inf->pspace->exec_bfd ();
+
   if (info.abfd == NULL)
-    info.abfd = core_bfd;
+    info.abfd = inf->pspace->cbfd.get ();
 
   /* Check for the current target description.  */
   if (info.target_desc == NULL)
-    info.target_desc = target_current_description (current_inferior ());
+    info.target_desc = target_current_description (inf);
 
   new_gdbarch = gdbarch_find_by_info (info);
 
@@ -601,7 +602,7 @@  gdbarch_update_p (struct gdbarch_info info)
 
   /* If it is the same old architecture, accept the request (but don't
      swap anything).  */
-  if (new_gdbarch == target_gdbarch ())
+  if (new_gdbarch == inf->gdbarch)
     {
       if (gdbarch_debug)
 	gdb_printf (gdb_stdlog, "gdbarch_update_p: "
@@ -741,7 +742,7 @@  initialize_current_architecture (void)
   info.byte_order = default_byte_order;
   info.byte_order_for_code = info.byte_order;
 
-  if (! gdbarch_update_p (info))
+  if (! gdbarch_update_p (current_inferior (), info))
     internal_error (_("initialize_current_architecture: Selection of "
 		      "initial architecture failed"));
 
diff --git a/gdb/arm-tdep.c b/gdb/arm-tdep.c
index 6f02f04b5cb..0badf3f2b1a 100644
--- a/gdb/arm-tdep.c
+++ b/gdb/arm-tdep.c
@@ -9393,7 +9393,7 @@  arm_update_current_architecture (void)
 
   /* Update the architecture.  */
   gdbarch_info info;
-  if (!gdbarch_update_p (info))
+  if (!gdbarch_update_p (current_inferior (), info))
     internal_error (_("could not update architecture"));
 }
 
diff --git a/gdb/cris-tdep.c b/gdb/cris-tdep.c
index d38850aa1af..7d2641a4eb0 100644
--- a/gdb/cris-tdep.c
+++ b/gdb/cris-tdep.c
@@ -3883,7 +3883,7 @@  set_cris_version (const char *ignore_args, int from_tty,
   usr_cmd_cris_version_valid = 1;
   
   /* Update the current architecture, if needed.  */
-  if (!gdbarch_update_p (info))
+  if (!gdbarch_update_p (current_inferior (), info))
     internal_error (_("cris_gdbarch_update: failed to update architecture."));
 }
 
@@ -3894,7 +3894,7 @@  set_cris_mode (const char *ignore_args, int from_tty,
   struct gdbarch_info info;
 
   /* Update the current architecture, if needed.  */
-  if (!gdbarch_update_p (info))
+  if (!gdbarch_update_p (current_inferior (), info))
     internal_error ("cris_gdbarch_update: failed to update architecture.");
 }
 
@@ -3905,7 +3905,7 @@  set_cris_dwarf2_cfi (const char *ignore_args, int from_tty,
   struct gdbarch_info info;
 
   /* Update the current architecture, if needed.  */
-  if (!gdbarch_update_p (info))
+  if (!gdbarch_update_p (current_inferior (), info))
     internal_error (_("cris_gdbarch_update: failed to update architecture."));
 }
 
diff --git a/gdb/gdbarch.h b/gdb/gdbarch.h
index 76ffddfe0ff..55e4a9b4743 100644
--- a/gdb/gdbarch.h
+++ b/gdb/gdbarch.h
@@ -319,7 +319,7 @@  extern obstack *gdbarch_obstack (gdbarch *arch);
 
 extern char *gdbarch_obstack_strdup (struct gdbarch *arch, const char *string);
 
-/* Helper function.  Force an update of the current architecture.
+/* Helper function.  Force an update of INF's current architecture.
 
    The actual architecture selected is determined by INFO, ``(gdb) set
    architecture'' et.al., the existing architecture and BFD's default
@@ -328,7 +328,7 @@  extern char *gdbarch_obstack_strdup (struct gdbarch *arch, const char *string);
 
    Returns non-zero if the update succeeds.  */
 
-extern int gdbarch_update_p (struct gdbarch_info info);
+extern int gdbarch_update_p (inferior *inf, struct gdbarch_info info);
 
 
 /* Helper function.  Find an architecture matching info.
diff --git a/gdb/i386-darwin-nat.c b/gdb/i386-darwin-nat.c
index b64b2b3e81b..b06d0007740 100644
--- a/gdb/i386-darwin-nat.c
+++ b/gdb/i386-darwin-nat.c
@@ -500,7 +500,7 @@  darwin_check_osabi (darwin_inferior *inf, thread_t thread)
       else
 	info.bfd_arch_info = bfd_lookup_arch (bfd_arch_i386,
 					      bfd_mach_i386_i386);
-      gdbarch_update_p (info);
+      gdbarch_update_p (current_inferior (), info);
     }
 }
 
diff --git a/gdb/mips-tdep.c b/gdb/mips-tdep.c
index 8c1643585f4..6474b8dc26e 100644
--- a/gdb/mips-tdep.c
+++ b/gdb/mips-tdep.c
@@ -894,7 +894,7 @@  set_mips64_transfers_32bit_regs (const char *args, int from_tty,
   /* FIXME: cagney/2003-11-15: Should be setting a field in "info"
      instead of relying on globals.  Doing that would let generic code
      handle the search for this specific architecture.  */
-  if (!gdbarch_update_p (info))
+  if (!gdbarch_update_p (current_inferior (), info))
     {
       mips64_transfers_32bit_regs_p = 0;
       error (_("32-bit compatibility mode not supported"));
@@ -6965,7 +6965,7 @@  set_mipsfpu_single_command (const char *args, int from_tty)
   /* FIXME: cagney/2003-11-15: Should be setting a field in "info"
      instead of relying on globals.  Doing that would let generic code
      handle the search for this specific architecture.  */
-  if (!gdbarch_update_p (info))
+  if (!gdbarch_update_p (current_inferior (), info))
     internal_error (_("set mipsfpu failed"));
 }
 
@@ -6978,7 +6978,7 @@  set_mipsfpu_double_command (const char *args, int from_tty)
   /* FIXME: cagney/2003-11-15: Should be setting a field in "info"
      instead of relying on globals.  Doing that would let generic code
      handle the search for this specific architecture.  */
-  if (!gdbarch_update_p (info))
+  if (!gdbarch_update_p (current_inferior (), info))
     internal_error (_("set mipsfpu failed"));
 }
 
@@ -6991,7 +6991,7 @@  set_mipsfpu_none_command (const char *args, int from_tty)
   /* FIXME: cagney/2003-11-15: Should be setting a field in "info"
      instead of relying on globals.  Doing that would let generic code
      handle the search for this specific architecture.  */
-  if (!gdbarch_update_p (info))
+  if (!gdbarch_update_p (current_inferior (), info))
     internal_error (_("set mipsfpu failed"));
 }
 
@@ -8836,7 +8836,7 @@  mips_abi_update (const char *ignore_args,
 
   /* Force the architecture to update, and (if it's a MIPS architecture)
      mips_gdbarch_init will take care of the rest.  */
-  gdbarch_update_p (info);
+  gdbarch_update_p (current_inferior (), info);
 }
 
 /* Print out which MIPS ABI is in use.  */
diff --git a/gdb/osabi.c b/gdb/osabi.c
index 57e2df6b25c..9c98c182fd7 100644
--- a/gdb/osabi.c
+++ b/gdb/osabi.c
@@ -24,6 +24,7 @@ 
 #include "gdbcmd.h"
 #include "command.h"
 #include "gdb_bfd.h"
+#include "inferior.h"
 
 #include "elf-bfd.h"
 
@@ -647,7 +648,7 @@  set_osabi (const char *args, int from_tty, struct cmd_list_element *c)
   /* NOTE: At some point (true multiple architectures) we'll need to be more
      graceful here.  */
   gdbarch_info info;
-  if (! gdbarch_update_p (info))
+  if (! gdbarch_update_p (current_inferior (), info))
     internal_error (_("Updating OS ABI failed."));
 }
 
diff --git a/gdb/rs6000-aix-nat.c b/gdb/rs6000-aix-nat.c
index 2ac1f6e70b6..b9ae6ea405c 100644
--- a/gdb/rs6000-aix-nat.c
+++ b/gdb/rs6000-aix-nat.c
@@ -765,7 +765,7 @@  rs6000_nat_target::create_inferior (const char *exec_file,
   info.bfd_arch_info = bfd_get_arch_info (&abfd);
   info.abfd = current_program_space->exec_bfd ();
 
-  if (!gdbarch_update_p (info))
+  if (!gdbarch_update_p (current_inferior (), info))
     internal_error (_("rs6000_create_inferior: failed "
 		      "to select architecture"));
 }
diff --git a/gdb/rs6000-tdep.c b/gdb/rs6000-tdep.c
index cbd84514795..5991effa03e 100644
--- a/gdb/rs6000-tdep.c
+++ b/gdb/rs6000-tdep.c
@@ -8479,7 +8479,7 @@  powerpc_set_soft_float (const char *args, int from_tty,
   struct gdbarch_info info;
 
   /* Update the architecture.  */
-  if (!gdbarch_update_p (info))
+  if (!gdbarch_update_p (current_inferior (), info))
     internal_error (_("could not update architecture"));
 }
 
@@ -8505,7 +8505,7 @@  powerpc_set_vector_abi (const char *args, int from_tty,
 
   /* Update the architecture.  */
   gdbarch_info info;
-  if (!gdbarch_update_p (info))
+  if (!gdbarch_update_p (current_inferior (), info))
     internal_error (_("could not update architecture"));
 }
 
diff --git a/gdb/target-descriptions.c b/gdb/target-descriptions.c
index 0d50aadddb8..57d23747f26 100644
--- a/gdb/target-descriptions.c
+++ b/gdb/target-descriptions.c
@@ -564,7 +564,7 @@  target_find_description (void)
       struct gdbarch_info info;
 
       info.target_desc = tdesc_info->tdesc;
-      if (!gdbarch_update_p (info))
+      if (!gdbarch_update_p (current_inferior (), info))
 	warning (_("Architecture rejected target-supplied description"));
       else
 	{
@@ -598,7 +598,7 @@  target_clear_description (void)
   tdesc_info->tdesc = nullptr;
 
   gdbarch_info info;
-  if (!gdbarch_update_p (info))
+  if (!gdbarch_update_p (current_inferior (), info))
     internal_error (_("Could not remove target-supplied description"));
 }