public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Remove 'target_mach' global from score-tdep.c
@ 2022-03-15 22:31 Tom Tromey
  2022-03-16 11:38 ` Andrew Burgess
  2022-03-16 12:07 ` Pedro Alves
  0 siblings, 2 replies; 7+ messages in thread
From: Tom Tromey @ 2022-03-15 22:31 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

I randomly noticed that score-tdep.c sets a global variable from
score_gdbarch_init, and then reuses this in other spots in the file.

This seems incorrect to me, at least if multiple inferiors or targets
are used; or potentially if there is a single gdb session that
switches back and forth between (sub-)architectures, causing the
global to be invalid.

This patch fixes the problem by deferring the lookup.

I wrote this just based on inspection, though, and have no way to test
it.  I wonder if other *-tdep.c files have similar issues.
---
 gdb/score-tdep.c | 26 ++++++++++++++++----------
 1 file changed, 16 insertions(+), 10 deletions(-)

diff --git a/gdb/score-tdep.c b/gdb/score-tdep.c
index e20bee46df9..b5bc47ab694 100644
--- a/gdb/score-tdep.c
+++ b/gdb/score-tdep.c
@@ -53,13 +53,18 @@ struct score_frame_cache
   trad_frame_saved_reg *saved_regs;
 };
 
-static int target_mach = bfd_mach_score7;
+/* Return the bfd_mach_* value for GDBARCH.  */
+static int
+target_mach (struct gdbarch *gdbarch)
+{
+  return gdbarch_bfd_arch_info (gdbarch)->mach;
+}
 
 static struct type *
 score_register_type (struct gdbarch *gdbarch, int regnum)
 {
   gdb_assert (regnum >= 0 
-	      && regnum < ((target_mach == bfd_mach_score7)
+	      && regnum < ((target_mach (gdbarch) == bfd_mach_score7)
 			   ? SCORE7_NUM_REGS : SCORE3_NUM_REGS));
   return builtin_type (gdbarch)->builtin_uint32;
 }
@@ -108,7 +113,7 @@ static int
 score_register_sim_regno (struct gdbarch *gdbarch, int regnum)
 {
   gdb_assert (regnum >= 0 
-	      && regnum < ((target_mach == bfd_mach_score7)
+	      && regnum < ((target_mach (gdbarch) == bfd_mach_score7)
 			   ? SCORE7_NUM_REGS : SCORE3_NUM_REGS));
   return regnum;
 }
@@ -388,7 +393,7 @@ score_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
 {
   CORE_ADDR adjust_pc = bpaddr; 
 
-  if (target_mach == bfd_mach_score3)
+  if (target_mach (gdbarch) == bfd_mach_score3)
     score3_adjust_pc_and_fetch_inst (&adjust_pc, NULL,
 		    		     gdbarch_byte_order (gdbarch));
   else
@@ -404,13 +409,14 @@ score_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
 }
 
 static void
-score_xfer_register (struct regcache *regcache, int regnum, int length,
+score_xfer_register (struct gdbarch *gdbarch,
+		     struct regcache *regcache, int regnum, int length,
 		     enum bfd_endian endian, gdb_byte *readbuf,
 		     const gdb_byte *writebuf, int buf_offset)
 {
   int reg_offset = 0;
   gdb_assert (regnum >= 0 
-	      && regnum < ((target_mach == bfd_mach_score7)
+	      && regnum < ((target_mach (gdbarch) == bfd_mach_score7)
 			   ? SCORE7_NUM_REGS : SCORE3_NUM_REGS));
 
   switch (endian)
@@ -458,7 +464,7 @@ score_return_value (struct gdbarch *gdbarch, struct value *function,
 
 	  if (offset + xfer > TYPE_LENGTH (type))
 	    xfer = TYPE_LENGTH (type) - offset;
-	  score_xfer_register (regcache, regnum, xfer,
+	  score_xfer_register (gdbarch, regcache, regnum, xfer,
 			       gdbarch_byte_order(gdbarch),
 			       readbuf, writebuf, offset);
 	}
@@ -1317,7 +1323,8 @@ score_make_prologue_cache (struct frame_info *this_frame, void **this_cache)
     if (start_addr == 0)
       return cache;
 
-    if (target_mach == bfd_mach_score3)
+    struct gdbarch *gdbarch = get_frame_arch (this_frame);
+    if (target_mach (gdbarch) == bfd_mach_score3)
       score3_analyze_prologue (start_addr, pc, this_frame,
 			       (struct score_frame_cache *) *this_cache);
     else
@@ -1447,7 +1454,6 @@ static struct gdbarch *
 score_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
 {
   struct gdbarch *gdbarch;
-  target_mach = info.bfd_arch_info->mach;
 
   arches = gdbarch_list_lookup_by_info (arches, &info);
   if (arches != NULL)
@@ -1472,7 +1478,7 @@ score_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
   set_gdbarch_frame_align (gdbarch, score_frame_align);
   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
 
-  switch (target_mach)
+  switch (info.bfd_arch_info->mach)
     {
     case bfd_mach_score7:
       set_gdbarch_breakpoint_kind_from_pc (gdbarch,
-- 
2.34.1


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

end of thread, other threads:[~2022-03-17 15:42 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-15 22:31 [PATCH] Remove 'target_mach' global from score-tdep.c Tom Tromey
2022-03-16 11:38 ` Andrew Burgess
2022-03-16 12:07 ` Pedro Alves
2022-03-16 14:44   ` [PATCH] gdb: Remove support for S+core Pedro Alves
2022-03-16 15:55     ` Andrew Burgess
2022-03-16 16:28     ` Tom Tromey
2022-03-17 15:42     ` Pedro Alves

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