public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH] Remove 'target_mach' global from score-tdep.c
Date: Tue, 15 Mar 2022 16:31:54 -0600	[thread overview]
Message-ID: <20220315223154.6517-1-tom@tromey.com> (raw)

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


             reply	other threads:[~2022-03-15 22:31 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-15 22:31 Tom Tromey [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220315223154.6517-1-tom@tromey.com \
    --to=tom@tromey.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).