public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 0/3] Three random cleanups
@ 2023-11-06 16:33 Simon Marchi
  2023-11-06 16:33 ` [PATCH 1/3] gdb: remove regcache's address space Simon Marchi
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Simon Marchi @ 2023-11-06 16:33 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

I have been working no a patch to remove program_space::aspace, which I
have reminded about by reading this series:

  https://inbox.sourceware.org/gdb-patches/20231104155757.16649-1-tdevries@suse.de/T/#t

These 3 patches are preparatory patches I made for my "remove
program_space::aspace" patch, but I think they are good on their own,
so rather than delay posting them, here they are now.

Simon Marchi (3):
  gdb: remove regcache's address space
  gdb: remove get_current_regcache
  gdb: pass address_space to target dcache functions

 gdb/aarch64-fbsd-tdep.c      |  3 +-
 gdb/amd64-fbsd-tdep.c        |  6 +--
 gdb/arc-tdep.c               |  6 +--
 gdb/arm-fbsd-tdep.c          |  6 +--
 gdb/breakpoint.c             |  2 +-
 gdb/bsd-kvm.c                |  6 +--
 gdb/corelow.c                |  2 +-
 gdb/darwin-nat.c             |  7 ++--
 gdb/dcache.c                 |  6 +--
 gdb/displaced-stepping.c     |  3 +-
 gdb/frame.c                  | 17 +++++----
 gdb/frv-tdep.c               |  2 +-
 gdb/gcore-elf.c              |  5 +--
 gdb/i386-fbsd-nat.c          |  2 +-
 gdb/i386-fbsd-tdep.c         |  6 +--
 gdb/i386-tdep.c              |  3 +-
 gdb/ia64-linux-nat.c         |  2 +-
 gdb/infcall.c                |  7 ++--
 gdb/infcmd.c                 |  5 ++-
 gdb/infrun.c                 | 72 +++++++++++++++++------------------
 gdb/linux-fork.c             | 10 +++--
 gdb/linux-nat.c              | 56 +++++++++------------------
 gdb/linux-nat.h              |  2 -
 gdb/m32c-tdep.c              |  2 +-
 gdb/memattr.c                |  7 ++--
 gdb/mep-tdep.c               |  4 +-
 gdb/mi/mi-main.c             |  3 +-
 gdb/or1k-tdep.c              |  2 +-
 gdb/ppc-fbsd-tdep.c          |  6 +--
 gdb/proc-service.c           |  3 +-
 gdb/process-stratum-target.c | 14 -------
 gdb/process-stratum-target.h |  4 --
 gdb/procfs.c                 |  2 +-
 gdb/record-btrace.c          |  2 +-
 gdb/record-full.c            | 26 ++++++-------
 gdb/regcache-dump.c          |  5 ++-
 gdb/regcache.c               | 74 +++++++++++++-----------------------
 gdb/regcache.h               | 28 +++-----------
 gdb/remote.c                 |  8 ++--
 gdb/reverse.c                |  7 ++--
 gdb/riscv-fbsd-tdep.c        |  6 +--
 gdb/sol-thread.c             | 20 ++++------
 gdb/solib-darwin.c           |  6 +--
 gdb/solib-svr4.c             |  7 ++--
 gdb/stack.c                  |  6 +--
 gdb/symfile.c                |  2 +-
 gdb/target-dcache.c          | 24 ++++++------
 gdb/target-dcache.h          | 10 +++--
 gdb/target-delegates.c       | 28 --------------
 gdb/target.c                 | 24 +++---------
 gdb/target.h                 |  8 ----
 gdb/top.c                    |  2 +-
 gdb/tracepoint.c             |  6 +--
 gdb/windows-nat.c            |  4 +-
 gdb/xtensa-tdep.c            |  6 ++-
 55 files changed, 227 insertions(+), 365 deletions(-)


base-commit: 169fe7ab54b964a6619c6fd05ab55090fa671b10
-- 
2.42.0


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

* [PATCH 1/3] gdb: remove regcache's address space
  2023-11-06 16:33 [PATCH 0/3] Three random cleanups Simon Marchi
@ 2023-11-06 16:33 ` Simon Marchi
  2023-11-06 16:33 ` [PATCH 2/3] gdb: remove get_current_regcache Simon Marchi
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Simon Marchi @ 2023-11-06 16:33 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

While looking at the regcache code, I noticed that the address space
(passed to regcache when constructing it, and available through
regcache::aspace) wasn't relevant for the regcache itself.  Callers of
regcache::aspace use that method because it appears to be a convenient
way of getting the address space for a thread, if you already have the
regcache.  But there is always another way to get the address space, as
the callers pretty much always know which thread they are dealing with.
The regcache code itself doesn't use the address space.

This patch removes anything related to address_space from the regcache
code, and updates callers to get it from the thread in context.  This
removes a bit of unnecessary complexity from the regcache code.

The current get_thread_arch_regcache function gets an address_space for
the given thread using the target_thread_address_space function (which
calls the target_ops::thread_address_space method).  This suggest that
there might have been the intention of supporting per-thread address
spaces.  But digging through the history, I did not find any such case.
Maybe this method was just added because we needed a way to get an
address space from a ptid (because constructing a regcache required an
address space), and this seemed like the right way to do it, I don't
know.

The only implementations of thread_address_space and
process_stratum_target::thread_address_space and
linux_nat_target::thread_address_space, which essentially just return
the inferior's address space.  And thread_address_space is only used in
the current get_thread_arch_regcache, which gets removed.  So, I think
that the thread_address_space target method can be removed, and we can
assume that it's fine to use the inferior's address space everywhere.
Callers of regcache::aspace are updated to get the address space from
the relevant inferior, either using some context they already know
about, or in last resort using the current global context.

So, to summarize:

 - remove everything in regcache related to address spaces
 - in particular, remove get_thread_arch_regcache, and rename
   get_thread_arch_aspace_regcache to get_thread_arch_regcache
 - remove target_ops::thread_address_space, and
   target_thread_address_space
 - adjust all users of regcache::aspace to get the address space another
   way

Change-Id: I04fd41b22c83fe486522af7851c75bcfb31c88c7
---
 gdb/aarch64-fbsd-tdep.c      |  3 +-
 gdb/amd64-fbsd-tdep.c        |  6 ++--
 gdb/arm-fbsd-tdep.c          |  6 ++--
 gdb/darwin-nat.c             |  7 ++--
 gdb/displaced-stepping.c     |  3 +-
 gdb/frame.c                  | 12 ++++---
 gdb/gcore-elf.c              |  5 ++-
 gdb/i386-fbsd-tdep.c         |  6 ++--
 gdb/infrun.c                 | 43 +++++++++++------------
 gdb/linux-nat.c              | 56 +++++++++--------------------
 gdb/linux-nat.h              |  2 --
 gdb/ppc-fbsd-tdep.c          |  6 ++--
 gdb/proc-service.c           |  3 +-
 gdb/process-stratum-target.c | 14 --------
 gdb/process-stratum-target.h |  4 ---
 gdb/record-full.c            |  6 ++--
 gdb/regcache.c               | 68 ++++++++++++++----------------------
 gdb/regcache.h               | 27 +++-----------
 gdb/remote.c                 |  8 +++--
 gdb/riscv-fbsd-tdep.c        |  6 ++--
 gdb/sol-thread.c             | 20 +++++------
 gdb/solib-svr4.c             |  5 ++-
 gdb/target-delegates.c       | 28 ---------------
 gdb/target.c                 | 13 -------
 gdb/target.h                 |  8 -----
 25 files changed, 113 insertions(+), 252 deletions(-)

diff --git a/gdb/aarch64-fbsd-tdep.c b/gdb/aarch64-fbsd-tdep.c
index 1102eb0d13b8..681149b1c6ce 100644
--- a/gdb/aarch64-fbsd-tdep.c
+++ b/gdb/aarch64-fbsd-tdep.c
@@ -215,8 +215,7 @@ aarch64_fbsd_get_thread_local_address (struct gdbarch *gdbarch, ptid_t ptid,
   aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
   struct regcache *regcache;
 
-  regcache = get_thread_arch_regcache (current_inferior ()->process_target (),
-				       ptid, gdbarch);
+  regcache = get_thread_arch_regcache (current_inferior (), ptid, gdbarch);
 
   target_fetch_registers (regcache, tdep->tls_regnum_base);
 
diff --git a/gdb/amd64-fbsd-tdep.c b/gdb/amd64-fbsd-tdep.c
index 2b633cd479cc..5b0896fcd417 100644
--- a/gdb/amd64-fbsd-tdep.c
+++ b/gdb/amd64-fbsd-tdep.c
@@ -288,10 +288,8 @@ static CORE_ADDR
 amd64fbsd_get_thread_local_address (struct gdbarch *gdbarch, ptid_t ptid,
 				    CORE_ADDR lm_addr, CORE_ADDR offset)
 {
-  struct regcache *regcache;
-
-  regcache = get_thread_arch_regcache (current_inferior ()->process_target (),
-				       ptid, gdbarch);
+  regcache *regcache
+    = get_thread_arch_regcache (current_inferior (), ptid, gdbarch);
 
   target_fetch_registers (regcache, AMD64_FSBASE_REGNUM);
 
diff --git a/gdb/arm-fbsd-tdep.c b/gdb/arm-fbsd-tdep.c
index b6aa42a4fd05..90273a3f5ef8 100644
--- a/gdb/arm-fbsd-tdep.c
+++ b/gdb/arm-fbsd-tdep.c
@@ -271,10 +271,8 @@ arm_fbsd_get_thread_local_address (struct gdbarch *gdbarch, ptid_t ptid,
 				   CORE_ADDR lm_addr, CORE_ADDR offset)
 {
   arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
-  struct regcache *regcache;
-
-  regcache = get_thread_arch_regcache (current_inferior ()->process_target (),
-				       ptid, gdbarch);
+  regcache *regcache
+    = get_thread_arch_regcache (current_inferior (), ptid, gdbarch);
 
   target_fetch_registers (regcache, tdep->tls_regnum);
 
diff --git a/gdb/darwin-nat.c b/gdb/darwin-nat.c
index 588e9e2a3457..0de476b022bb 100644
--- a/gdb/darwin-nat.c
+++ b/gdb/darwin-nat.c
@@ -1151,7 +1151,7 @@ darwin_nat_target::decode_message (mach_msg_header_t *hdr,
 }
 
 int
-darwin_nat_target::cancel_breakpoint (ptid_t ptid)
+darwin_nat_target::cancel_breakpoint (inferior *inf, ptid_t ptid)
 {
   /* Arrange for a breakpoint to be hit again later.  We will handle
      the current event, eventually we will resume this thread, and this
@@ -1166,7 +1166,7 @@ darwin_nat_target::cancel_breakpoint (ptid_t ptid)
   CORE_ADDR pc;
 
   pc = regcache_read_pc (regcache) - gdbarch_decr_pc_after_break (gdbarch);
-  if (breakpoint_inserted_here_p (regcache->aspace (), pc))
+  if (breakpoint_inserted_here_p (inf->aspace, pc))
     {
       inferior_debug (4, "cancel_breakpoint for thread 0x%lx\n",
 		      (unsigned long) ptid.tid ());
@@ -1286,7 +1286,8 @@ darwin_nat_target::wait_1 (ptid_t ptid, struct target_waitstatus *status)
 	  && thread->event.ex_type == EXC_BREAKPOINT)
 	{
 	  if (thread->single_step
-	      || cancel_breakpoint (ptid_t (inf->pid, 0, thread->gdb_port)))
+	      || cancel_breakpoint (inf,
+				    ptid_t (inf->pid, 0, thread->gdb_port)))
 	    {
 	      gdb_assert (thread->msg_state == DARWIN_MESSAGE);
 	      darwin_send_reply (inf, thread);
diff --git a/gdb/displaced-stepping.c b/gdb/displaced-stepping.c
index 41c3c999d1e9..6c90fd2c9b64 100644
--- a/gdb/displaced-stepping.c
+++ b/gdb/displaced-stepping.c
@@ -53,7 +53,6 @@ displaced_step_buffers::prepare (thread_info *thread, CORE_ADDR &displaced_pc)
     gdb_assert (buf.current_thread != thread);
 
   regcache *regcache = get_thread_regcache (thread);
-  const address_space *aspace = regcache->aspace ();
   gdbarch *arch = regcache->arch ();
   ULONGEST len = gdbarch_displaced_step_buffer_length (arch);
 
@@ -64,7 +63,7 @@ displaced_step_buffers::prepare (thread_info *thread, CORE_ADDR &displaced_pc)
 
   for (displaced_step_buffer &candidate : m_buffers)
     {
-      bool bp_in_range = breakpoint_in_range_p (aspace, candidate.addr, len);
+      bool bp_in_range = breakpoint_in_range_p (thread->inf->aspace, candidate.addr, len);
       bool is_free = candidate.current_thread == nullptr;
 
       if (!bp_in_range)
diff --git a/gdb/frame.c b/gdb/frame.c
index 7077016ccba4..2d63d6e4b4cc 100644
--- a/gdb/frame.c
+++ b/gdb/frame.c
@@ -1619,14 +1619,15 @@ put_frame_register_bytes (frame_info_ptr frame, int regnum,
    CODE_ADDR.  */
 
 static frame_info_ptr
-create_sentinel_frame (struct program_space *pspace, struct regcache *regcache,
-		       CORE_ADDR stack_addr, CORE_ADDR code_addr)
+create_sentinel_frame (program_space *pspace, address_space *aspace,
+		       regcache *regcache, CORE_ADDR stack_addr,
+		       CORE_ADDR code_addr)
 {
   frame_info *frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
 
   frame->level = -1;
   frame->pspace = pspace;
-  frame->aspace = regcache->aspace ();
+  frame->aspace = aspace;
   /* Explicitly initialize the sentinel frame's cache.  Provide it
      with the underlying regcache.  In the future additional
      information, such as the frame's thread will be added.  */
@@ -1687,8 +1688,8 @@ get_current_frame (void)
 
   if (sentinel_frame == NULL)
     sentinel_frame =
-      create_sentinel_frame (current_program_space, get_current_regcache (),
-			     0, 0).get ();
+      create_sentinel_frame (current_program_space, current_inferior ()->aspace,
+			     get_current_regcache (), 0, 0).get ();
 
   /* Set the current frame before computing the frame id, to avoid
      recursion inside compute_frame_id, in case the frame's
@@ -2021,6 +2022,7 @@ create_new_frame (frame_id id)
   frame_info *fi = FRAME_OBSTACK_ZALLOC (struct frame_info);
 
   fi->next = create_sentinel_frame (current_program_space,
+				    current_inferior ()->aspace,
 				    get_current_regcache (),
 				    id.stack_addr, id.code_addr).get ();
 
diff --git a/gdb/gcore-elf.c b/gdb/gcore-elf.c
index 0142db82670d..8b4177cfe257 100644
--- a/gdb/gcore-elf.c
+++ b/gdb/gcore-elf.c
@@ -128,9 +128,8 @@ gcore_elf_build_thread_register_notes
   (struct gdbarch *gdbarch, struct thread_info *info, gdb_signal stop_signal,
    bfd *obfd, gdb::unique_xmalloc_ptr<char> *note_data, int *note_size)
 {
-  struct regcache *regcache
-    = get_thread_arch_regcache (info->inf->process_target (),
-				info->ptid, gdbarch);
+  regcache *regcache
+    = get_thread_arch_regcache (info->inf, info->ptid, gdbarch);
   target_fetch_registers (regcache, -1);
   gcore_elf_collect_thread_registers (regcache, info->ptid, obfd,
 				      note_data, note_size, stop_signal);
diff --git a/gdb/i386-fbsd-tdep.c b/gdb/i386-fbsd-tdep.c
index f8c2758c2b0c..58d06e367670 100644
--- a/gdb/i386-fbsd-tdep.c
+++ b/gdb/i386-fbsd-tdep.c
@@ -354,10 +354,8 @@ static CORE_ADDR
 i386fbsd_get_thread_local_address (struct gdbarch *gdbarch, ptid_t ptid,
 				   CORE_ADDR lm_addr, CORE_ADDR offset)
 {
-  struct regcache *regcache;
-
-  regcache = get_thread_arch_regcache (current_inferior ()->process_target (),
-				       ptid, gdbarch);
+  regcache *regcache
+    = get_thread_arch_regcache (current_inferior (), ptid, gdbarch);
 
   target_fetch_registers (regcache, I386_GSBASE_REGNUM);
 
diff --git a/gdb/infrun.c b/gdb/infrun.c
index 4fde96800fb1..3a057764b149 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -2455,7 +2455,6 @@ resume_1 (enum gdb_signal sig)
   struct regcache *regcache = get_current_regcache ();
   struct gdbarch *gdbarch = regcache->arch ();
   struct thread_info *tp = inferior_thread ();
-  const address_space *aspace = regcache->aspace ();
   ptid_t resume_ptid;
   /* This represents the user's step vs continue request.  When
      deciding whether "set scheduler-locking step" applies, it's the
@@ -2534,6 +2533,8 @@ resume_1 (enum gdb_signal sig)
 		       inferior_ptid.to_string ().c_str (),
 		       paddress (gdbarch, pc));
 
+  const address_space *aspace = tp->inf->aspace;
+
   /* Normally, by the time we reach `resume', the breakpoints are either
      removed or inserted, as appropriate.  The exception is if we're sitting
      at a permanent breakpoint; we need to step over it, but permanent
@@ -2648,8 +2649,8 @@ resume_1 (enum gdb_signal sig)
 	  if (target_is_non_stop_p ())
 	    stop_all_threads ("displaced stepping falling back on inline stepping");
 
-	  set_step_over_info (regcache->aspace (),
-			      regcache_read_pc (regcache), 0, tp->global_num);
+	  set_step_over_info (aspace, regcache_read_pc (regcache), 0,
+			      tp->global_num);
 
 	  step = maybe_software_singlestep (gdbarch);
 
@@ -2976,7 +2977,7 @@ thread_still_needs_step_over_bp (struct thread_info *tp)
     {
       struct regcache *regcache = get_thread_regcache (tp);
 
-      if (breakpoint_here_p (regcache->aspace (),
+      if (breakpoint_here_p (tp->inf->aspace,
 			     regcache_read_pc (regcache))
 	  == ordinary_breakpoint_here)
 	return true;
@@ -3419,7 +3420,6 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal)
 
   regcache = get_current_regcache ();
   gdbarch = regcache->arch ();
-  const address_space *aspace = regcache->aspace ();
 
   pc = regcache_read_pc_protected (regcache);
 
@@ -3441,6 +3441,8 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal)
 
   if (addr == (CORE_ADDR) -1)
     {
+      const address_space *aspace = cur_thr->inf->aspace;
+
       if (cur_thr->stop_pc_p ()
 	  && pc == cur_thr->stop_pc ()
 	  && breakpoint_here_p (aspace, pc) == ordinary_breakpoint_here
@@ -3869,7 +3871,7 @@ do_target_wait_1 (inferior *inf, ptid_t ptid,
 			       paddress (gdbarch, pc));
 	  discard = 1;
 	}
-      else if (!breakpoint_inserted_here_p (regcache->aspace (), pc))
+      else if (!breakpoint_inserted_here_p (tp->inf->aspace, pc))
 	{
 	  infrun_debug_printf ("previous breakpoint of %s, at %s gone",
 			       tp->ptid.to_string ().c_str (),
@@ -4746,7 +4748,7 @@ adjust_pc_after_break (struct thread_info *thread,
   if (decr_pc == 0)
     return;
 
-  const address_space *aspace = regcache->aspace ();
+  const address_space *aspace = thread->inf->aspace;
 
   /* Find the location where (if we've hit a breakpoint) the
      breakpoint would be.  */
@@ -4898,7 +4900,7 @@ handle_syscall_event (struct execution_control_state *ecs)
       infrun_debug_printf ("syscall number=%d", syscall_number);
 
       ecs->event_thread->control.stop_bpstat
-	= bpstat_stop_status_nowatch (regcache->aspace (),
+	= bpstat_stop_status_nowatch (ecs->event_thread->inf->aspace,
 				      ecs->event_thread->stop_pc (),
 				      ecs->event_thread, ecs->ws);
 
@@ -5095,7 +5097,7 @@ save_waitstatus (struct thread_info *tp, const target_waitstatus &ws)
       && ws.sig () == GDB_SIGNAL_TRAP)
     {
       struct regcache *regcache = get_thread_regcache (tp);
-      const address_space *aspace = regcache->aspace ();
+      const address_space *aspace = tp->inf->aspace;
       CORE_ADDR pc = regcache_read_pc (regcache);
 
       adjust_pc_after_break (tp, tp->pending_waitstatus ());
@@ -5675,7 +5677,7 @@ handle_inferior_event (struct execution_control_state *ecs)
     {
       struct regcache *regcache = get_thread_regcache (ecs->event_thread);
 
-      if (breakpoint_inserted_here_p (regcache->aspace (),
+      if (breakpoint_inserted_here_p (ecs->event_thread->inf->aspace,
 				      regcache_read_pc (regcache)))
 	{
 	  infrun_debug_printf ("Treating signal as SIGTRAP");
@@ -5708,7 +5710,7 @@ handle_inferior_event (struct execution_control_state *ecs)
 
 	    ecs->event_thread->set_stop_pc (regcache_read_pc (regcache));
 	    ecs->event_thread->control.stop_bpstat
-	      = bpstat_stop_status_nowatch (regcache->aspace (),
+	      = bpstat_stop_status_nowatch (ecs->event_thread->inf->aspace,
 					    ecs->event_thread->stop_pc (),
 					    ecs->event_thread, ecs->ws);
 
@@ -5902,10 +5904,8 @@ handle_inferior_event (struct execution_control_state *ecs)
 	       list yet at this point.  */
 
 	    child_regcache
-	      = get_thread_arch_aspace_regcache (parent_inf,
-						 ecs->ws.child_ptid (),
-						 gdbarch,
-						 parent_inf->aspace);
+	      = get_thread_arch_regcache (parent_inf, ecs->ws.child_ptid (),
+					  gdbarch);
 	    /* Read PC value of parent process.  */
 	    parent_pc = regcache_read_pc (regcache);
 
@@ -5950,7 +5950,7 @@ handle_inferior_event (struct execution_control_state *ecs)
 	(regcache_read_pc (get_thread_regcache (ecs->event_thread)));
 
       ecs->event_thread->control.stop_bpstat
-	= bpstat_stop_status_nowatch (get_current_regcache ()->aspace (),
+	= bpstat_stop_status_nowatch (ecs->event_thread->inf->aspace,
 				      ecs->event_thread->stop_pc (),
 				      ecs->event_thread, ecs->ws);
 
@@ -6076,7 +6076,7 @@ handle_inferior_event (struct execution_control_state *ecs)
 	(regcache_read_pc (get_thread_regcache (ecs->event_thread)));
 
       ecs->event_thread->control.stop_bpstat
-	= bpstat_stop_status_nowatch (get_current_regcache ()->aspace (),
+	= bpstat_stop_status_nowatch (ecs->event_thread->inf->aspace,
 				      ecs->event_thread->stop_pc (),
 				      ecs->event_thread, ecs->ws);
 
@@ -6493,7 +6493,7 @@ handle_signal_stop (struct execution_control_state *ecs)
       CORE_ADDR pc;
 
       regcache = get_thread_regcache (ecs->event_thread);
-      const address_space *aspace = regcache->aspace ();
+      const address_space *aspace = ecs->event_thread->inf->aspace;
 
       pc = regcache_read_pc (regcache);
 
@@ -6577,8 +6577,7 @@ handle_signal_stop (struct execution_control_state *ecs)
      inline function call sites).  */
   if (ecs->event_thread->control.step_range_end != 1)
     {
-      const address_space *aspace
-	= get_thread_regcache (ecs->event_thread)->aspace ();
+      const address_space *aspace = ecs->event_thread->inf->aspace;
 
       /* skip_inline_frames is expensive, so we avoid it if we can
 	 determine that the address is one where functions cannot have
@@ -6656,7 +6655,7 @@ handle_signal_stop (struct execution_control_state *ecs)
   /* See if there is a breakpoint/watchpoint/catchpoint/etc. that
      handles this event.  */
   ecs->event_thread->control.stop_bpstat
-    = bpstat_stop_status (get_current_regcache ()->aspace (),
+    = bpstat_stop_status (ecs->event_thread->inf->aspace,
 			  ecs->event_thread->stop_pc (),
 			  ecs->event_thread, ecs->ws, stop_chain);
 
@@ -8527,7 +8526,7 @@ keep_going_pass_signal (struct execution_control_state *ecs)
       if (remove_bp
 	  && (remove_wps || !use_displaced_stepping (ecs->event_thread)))
 	{
-	  set_step_over_info (regcache->aspace (),
+	  set_step_over_info (ecs->event_thread->inf->aspace,
 			      regcache_read_pc (regcache), remove_wps,
 			      ecs->event_thread->global_num);
 	}
diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c
index 1c9756c18bd7..d215ead0f110 100644
--- a/gdb/linux-nat.c
+++ b/gdb/linux-nat.c
@@ -2430,6 +2430,17 @@ stop_wait_callback (struct lwp_info *lp)
   return 0;
 }
 
+/* Get the inferior associated to LWP.  Must be called with an LWP that has
+   an associated inferior.  Always return non-nullptr.  */
+
+static inferior *
+lwp_inferior (const lwp_info *lwp)
+{
+  inferior *inf = find_inferior_ptid (linux_target, lwp->ptid);
+  gdb_assert (inf != nullptr);
+  return inf;
+}
+
 /* Return non-zero if LP has a wait status pending.  Discard the
    pending event and resume the LWP if the event that originally
    caused the stop became uninteresting.  */
@@ -2465,7 +2476,7 @@ status_callback (struct lwp_info *lp)
 	}
 
 #if !USE_SIGTRAP_SIGINFO
-      else if (!breakpoint_inserted_here_p (regcache->aspace (), pc))
+      else if (!breakpoint_inserted_here_p (lwp_inferior (lp)->aspace, pc))
 	{
 	  linux_nat_debug_printf ("previous breakpoint of %s, at %s gone",
 				  lp->ptid.to_string ().c_str (),
@@ -2565,7 +2576,7 @@ save_stop_reason (struct lwp_info *lp)
   if (!linux_target->low_status_is_event (lp->status))
     return;
 
-  inferior *inf = find_inferior_ptid (linux_target, lp->ptid);
+  inferior *inf = lwp_inferior (lp);
   if (inf->starting_up)
     return;
 
@@ -2620,15 +2631,14 @@ save_stop_reason (struct lwp_info *lp)
     }
 #else
   if ((!lp->step || lp->stop_pc == sw_bp_pc)
-      && software_breakpoint_inserted_here_p (regcache->aspace (),
-					      sw_bp_pc))
+      && software_breakpoint_inserted_here_p (inf->aspace, sw_bp_pc))
     {
       /* The LWP was either continued, or stepped a software
 	 breakpoint instruction.  */
       lp->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
     }
 
-  if (hardware_breakpoint_inserted_here_p (regcache->aspace (), pc))
+  if (hardware_breakpoint_inserted_here_p (inf->aspace, pc))
     lp->stop_reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
 
   if (lp->stop_reason == TARGET_STOPPED_BY_NO_REASON)
@@ -3363,7 +3373,7 @@ linux_nat_wait_1 (ptid_t ptid, struct target_waitstatus *ourstatus,
 static int
 resume_stopped_resumed_lwps (struct lwp_info *lp, const ptid_t wait_ptid)
 {
-  inferior *inf = find_inferior_ptid (linux_target, lp->ptid);
+  inferior *inf = lwp_inferior (lp);
 
   if (!lp->stopped)
     {
@@ -3399,7 +3409,7 @@ resume_stopped_resumed_lwps (struct lwp_info *lp, const ptid_t wait_ptid)
 	     immediately, and we're not waiting for this LWP.  */
 	  if (!lp->ptid.matches (wait_ptid))
 	    {
-	      if (breakpoint_inserted_here_p (regcache->aspace (), pc))
+	      if (breakpoint_inserted_here_p (inf->aspace, pc))
 		leave_stopped = 1;
 	    }
 
@@ -4332,38 +4342,6 @@ linux_nat_target::stop (ptid_t ptid)
   iterate_over_lwps (ptid, linux_nat_stop_lwp);
 }
 
-/* When requests are passed down from the linux-nat layer to the
-   single threaded inf-ptrace layer, ptids of (lwpid,0,0) form are
-   used.  The address space pointer is stored in the inferior object,
-   but the common code that is passed such ptid can't tell whether
-   lwpid is a "main" process id or not (it assumes so).  We reverse
-   look up the "main" process id from the lwp here.  */
-
-struct address_space *
-linux_nat_target::thread_address_space (ptid_t ptid)
-{
-  struct lwp_info *lwp;
-  struct inferior *inf;
-  int pid;
-
-  if (ptid.lwp () == 0)
-    {
-      /* An (lwpid,0,0) ptid.  Look up the lwp object to get at the
-	 tgid.  */
-      lwp = find_lwp_pid (ptid);
-      pid = lwp->ptid.pid ();
-    }
-  else
-    {
-      /* A (pid,lwpid,0) ptid.  */
-      pid = ptid.pid ();
-    }
-
-  inf = find_inferior_pid (this, pid);
-  gdb_assert (inf != NULL);
-  return inf->aspace;
-}
-
 /* Return the cached value of the processor core for thread PTID.  */
 
 int
diff --git a/gdb/linux-nat.h b/gdb/linux-nat.h
index 770fe9244277..72c4bbefbb59 100644
--- a/gdb/linux-nat.h
+++ b/gdb/linux-nat.h
@@ -68,8 +68,6 @@ class linux_nat_target : public inf_ptrace_target
 
   const char *thread_name (struct thread_info *) override;
 
-  struct address_space *thread_address_space (ptid_t) override;
-
   bool stopped_by_watchpoint () override;
 
   bool stopped_data_address (CORE_ADDR *) override;
diff --git a/gdb/ppc-fbsd-tdep.c b/gdb/ppc-fbsd-tdep.c
index 4355b6483fea..2238a22afd90 100644
--- a/gdb/ppc-fbsd-tdep.c
+++ b/gdb/ppc-fbsd-tdep.c
@@ -288,11 +288,9 @@ ppcfbsd_get_thread_local_address (struct gdbarch *gdbarch, ptid_t ptid,
 				  CORE_ADDR lm_addr, CORE_ADDR offset)
 {
   ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
-  struct regcache *regcache;
   int tp_offset, tp_regnum;
-
-  regcache = get_thread_arch_regcache (current_inferior ()->process_target (),
-				       ptid, gdbarch);
+  regcache *regcache
+    = get_thread_arch_regcache (current_inferior (), ptid, gdbarch);
 
   if (tdep->wordsize == 4)
     {
diff --git a/gdb/proc-service.c b/gdb/proc-service.c
index e41278dbe6a8..48f3dec55871 100644
--- a/gdb/proc-service.c
+++ b/gdb/proc-service.c
@@ -135,8 +135,7 @@ static struct regcache *
 get_ps_regcache (struct ps_prochandle *ph, lwpid_t lwpid)
 {
   inferior *inf = ph->thread->inf;
-  return get_thread_arch_regcache (inf->process_target (),
-				   ptid_t (inf->pid, lwpid),
+  return get_thread_arch_regcache (inf, ptid_t (inf->pid, lwpid),
 				   inf->arch ());
 }
 
diff --git a/gdb/process-stratum-target.c b/gdb/process-stratum-target.c
index 173c3ecad134..017d26e28df1 100644
--- a/gdb/process-stratum-target.c
+++ b/gdb/process-stratum-target.c
@@ -26,20 +26,6 @@ process_stratum_target::~process_stratum_target ()
 {
 }
 
-struct address_space *
-process_stratum_target::thread_address_space (ptid_t ptid)
-{
-  /* Fall-back to the "main" address space of the inferior.  */
-  inferior *inf = find_inferior_ptid (this, ptid);
-
-  if (inf == NULL || inf->aspace == NULL)
-    internal_error (_("Can't determine the current "
-		      "address space of thread %s\n"),
-		    target_pid_to_str (ptid).c_str ());
-
-  return inf->aspace;
-}
-
 struct gdbarch *
 process_stratum_target::thread_architecture (ptid_t ptid)
 {
diff --git a/gdb/process-stratum-target.h b/gdb/process-stratum-target.h
index f4968ec47019..f35b59978f82 100644
--- a/gdb/process-stratum-target.h
+++ b/gdb/process-stratum-target.h
@@ -51,10 +51,6 @@ class process_stratum_target : public target_ops
   bool supports_non_stop () override { return false; }
   bool supports_disable_randomization () override { return false; }
 
-  /* This default implementation returns the inferior's address
-     space.  */
-  struct address_space *thread_address_space (ptid_t ptid) override;
-
   /* This default implementation always returns the current inferior's
      gdbarch.  */
   struct gdbarch *thread_architecture (ptid_t ptid) override;
diff --git a/gdb/record-full.c b/gdb/record-full.c
index 2ee08e6d54fb..1cea869e88bb 100644
--- a/gdb/record-full.c
+++ b/gdb/record-full.c
@@ -884,7 +884,7 @@ record_full_exec_insn (struct regcache *regcache,
 		       not doing the change at all if the watchpoint
 		       traps.  */
 		    if (hardware_watchpoint_inserted_in_range
-			(regcache->aspace (),
+			(current_inferior ()->aspace,
 			 entry->u.mem.addr, entry->u.mem.len))
 		      record_full_stop_reason = TARGET_STOPPED_BY_WATCHPOINT;
 		  }
@@ -1218,7 +1218,7 @@ record_full_wait_1 (struct target_ops *ops,
 				    ret);
 		  regcache = get_current_regcache ();
 		  tmp_pc = regcache_read_pc (regcache);
-		  const struct address_space *aspace = regcache->aspace ();
+		  const address_space *aspace = current_inferior ()->aspace;
 
 		  if (target_stopped_by_watchpoint ())
 		    {
@@ -1288,7 +1288,7 @@ record_full_wait_1 (struct target_ops *ops,
 			record_full_resume_ptid);
       struct regcache *regcache = get_current_regcache ();
       struct gdbarch *gdbarch = regcache->arch ();
-      const struct address_space *aspace = regcache->aspace ();
+      const address_space *aspace = current_inferior ()->aspace;
       int continue_flag = 1;
       int first_record_full_end = 1;
 
diff --git a/gdb/regcache.c b/gdb/regcache.c
index 2e48c0258091..48a926ed4483 100644
--- a/gdb/regcache.c
+++ b/gdb/regcache.c
@@ -208,11 +208,10 @@ reg_buffer::reg_buffer (gdbarch *gdbarch, bool has_pseudo)
     }
 }
 
-regcache::regcache (inferior *inf_for_target_calls, gdbarch *gdbarch,
-		    const address_space *aspace_)
+regcache::regcache (inferior *inf_for_target_calls, gdbarch *gdbarch)
 /* The register buffers.  A read/write register cache can only hold
    [0 .. gdbarch_num_regs).  */
-  : detached_regcache (gdbarch, false), m_aspace (aspace_),
+  : detached_regcache (gdbarch, false),
     m_inf_for_target_calls (inf_for_target_calls)
 {
   m_ptid = minus_one_ptid;
@@ -348,10 +347,9 @@ using target_pid_ptid_regcache_map
    target when appropriate.  */
 static target_pid_ptid_regcache_map regcaches;
 
-struct regcache *
-get_thread_arch_aspace_regcache (inferior *inf_for_target_calls,
-				 ptid_t ptid, gdbarch *arch,
-				 struct address_space *aspace)
+regcache *
+get_thread_arch_regcache (inferior *inf_for_target_calls, ptid_t ptid,
+			  gdbarch *arch)
 {
   gdb_assert (inf_for_target_calls != nullptr);
 
@@ -373,7 +371,7 @@ get_thread_arch_aspace_regcache (inferior *inf_for_target_calls,
     }
 
   /* It does not exist, create it.  */
-  regcache *new_regcache = new regcache (inf_for_target_calls, arch, aspace);
+  regcache *new_regcache = new regcache (inf_for_target_calls, arch);
   new_regcache->set_ptid (ptid);
   /* Work around a problem with g++ 4.8 (PR96537): Call the regcache_up
      constructor explicitly instead of implicitly.  */
@@ -382,18 +380,6 @@ get_thread_arch_aspace_regcache (inferior *inf_for_target_calls,
   return new_regcache;
 }
 
-struct regcache *
-get_thread_arch_regcache (process_stratum_target *target, ptid_t ptid,
-			  struct gdbarch *gdbarch)
-{
-  scoped_restore_current_inferior restore_current_inferior;
-  inferior *inf = find_inferior_ptid (target, ptid);
-  set_current_inferior (inf);
-  address_space *aspace = target_thread_address_space (ptid);
-
-  return get_thread_arch_aspace_regcache (inf, ptid, gdbarch, aspace);
-}
-
 static process_stratum_target *current_thread_target;
 static ptid_t current_thread_ptid;
 static struct gdbarch *current_thread_arch;
@@ -401,6 +387,8 @@ static struct gdbarch *current_thread_arch;
 struct regcache *
 get_thread_regcache (process_stratum_target *target, ptid_t ptid)
 {
+  inferior *inf = find_inferior_ptid (target, ptid);
+
   if (!current_thread_arch
       || target != current_thread_target
       || current_thread_ptid != ptid)
@@ -411,11 +399,11 @@ get_thread_regcache (process_stratum_target *target, ptid_t ptid)
       current_thread_target = target;
 
       scoped_restore_current_inferior restore_current_inferior;
-      set_current_inferior (find_inferior_ptid (target, ptid));
+      set_current_inferior (inf);
       current_thread_arch = target_thread_architecture (ptid);
     }
 
-  return get_thread_arch_regcache (target, ptid, current_thread_arch);
+  return get_thread_arch_regcache (inf, ptid, current_thread_arch);
 }
 
 /* See regcache.h.  */
@@ -1618,24 +1606,22 @@ regcache_count (process_stratum_target *target, ptid_t ptid)
   return 0;
 };
 
-/* Wrapper around get_thread_arch_aspace_regcache that does some self checks.  */
+/* Wrapper around get_thread_arch_regcache that does some self checks.  */
 
 static void
-get_thread_arch_aspace_regcache_and_check (inferior *inf_for_target_calls,
-					   ptid_t ptid)
+get_thread_arch_regcache_and_check (inferior *inf_for_target_calls,
+				    ptid_t ptid)
 {
   /* We currently only test with a single gdbarch.  Any gdbarch will do, so use
      the current inferior's gdbarch.  Also use the current inferior's address
      space.  */
   gdbarch *arch = inf_for_target_calls->arch ();
-  address_space *aspace = inf_for_target_calls->aspace;
-  regcache *regcache = get_thread_arch_aspace_regcache (inf_for_target_calls,
-							ptid, arch, aspace);
+  regcache *regcache
+    = get_thread_arch_regcache (inf_for_target_calls, ptid, arch);
 
   SELF_CHECK (regcache != NULL);
   SELF_CHECK (regcache->ptid () == ptid);
   SELF_CHECK (regcache->arch () == arch);
-  SELF_CHECK (regcache->aspace () == aspace);
 }
 
 /* The data that the regcaches selftests must hold onto for the duration of the
@@ -1683,12 +1669,12 @@ populate_regcaches_for_test ()
     {
       for (long lwp : { 1, 2, 3 })
 	{
-	  get_thread_arch_aspace_regcache_and_check
+	  get_thread_arch_regcache_and_check
 	    (&data->test_ctx_1.mock_inferior, ptid_t (pid, lwp));
 	  expected_regcache_size++;
 	  SELF_CHECK (regcaches_size () == expected_regcache_size);
 
-	  get_thread_arch_aspace_regcache_and_check
+	  get_thread_arch_regcache_and_check
 	    (&data->test_ctx_2.mock_inferior, ptid_t (pid, lwp));
 	  expected_regcache_size++;
 	  SELF_CHECK (regcaches_size () == expected_regcache_size);
@@ -1699,16 +1685,16 @@ populate_regcaches_for_test ()
 }
 
 static void
-get_thread_arch_aspace_regcache_test ()
+get_thread_arch_regcache_test ()
 {
   /* populate_regcaches_for_test already tests most of the
-     get_thread_arch_aspace_regcache functionality.  */
+     get_thread_arch_regcache functionality.  */
   regcache_test_data_up data = populate_regcaches_for_test ();
   size_t regcaches_size_before = regcaches_size ();
 
   /* Test that getting an existing regcache doesn't create a new one.  */
-  get_thread_arch_aspace_regcache_and_check (&data->test_ctx_1.mock_inferior,
-					     ptid_t (2, 2));
+  get_thread_arch_regcache_and_check (&data->test_ctx_1.mock_inferior,
+				      ptid_t (2, 2));
   SELF_CHECK (regcaches_size () == regcaches_size_before);
 }
 
@@ -1835,7 +1821,7 @@ class readwrite_regcache : public regcache
 public:
   readwrite_regcache (inferior *inf_for_target_calls,
 		      struct gdbarch *gdbarch)
-    : regcache (inf_for_target_calls, gdbarch, nullptr)
+    : regcache (inf_for_target_calls, gdbarch)
   {}
 };
 
@@ -2116,10 +2102,8 @@ regcache_thread_ptid_changed ()
   gdb_assert (regcaches.empty ());
 
   /* Populate the regcaches container.  */
-  get_thread_arch_aspace_regcache (&target1.mock_inferior, old_ptid, arch,
-				   nullptr);
-  get_thread_arch_aspace_regcache (&target2.mock_inferior, old_ptid, arch,
-				   nullptr);
+  get_thread_arch_regcache (&target1.mock_inferior, old_ptid, arch);
+  get_thread_arch_regcache (&target2.mock_inferior, old_ptid, arch);
 
   gdb_assert (regcaches.size () == 2);
   gdb_assert (regcache_count (&target1.mock_target, old_ptid) == 1);
@@ -2163,8 +2147,8 @@ _initialize_regcache ()
   deprecate_cmd (c, "maintenance flush register-cache");
 
 #if GDB_SELF_TEST
-  selftests::register_test ("get_thread_arch_aspace_regcache",
-			    selftests::get_thread_arch_aspace_regcache_test);
+  selftests::register_test ("get_thread_arch_regcache",
+			    selftests::get_thread_arch_regcache_test);
   selftests::register_test ("registers_changed_ptid_all",
 			    selftests::registers_changed_ptid_all_test);
   selftests::register_test ("registers_changed_ptid_target",
diff --git a/gdb/regcache.h b/gdb/regcache.h
index 57ddac465f09..f73f08a44ab7 100644
--- a/gdb/regcache.h
+++ b/gdb/regcache.h
@@ -26,7 +26,6 @@
 struct regcache;
 struct regset;
 struct gdbarch;
-struct address_space;
 class thread_info;
 struct process_stratum_target;
 struct inferior;
@@ -38,11 +37,8 @@ extern struct regcache *get_thread_regcache (process_stratum_target *target,
 /* Get the regcache of THREAD.  */
 extern struct regcache *get_thread_regcache (thread_info *thread);
 
-extern struct regcache *get_thread_arch_regcache
-  (process_stratum_target *targ, ptid_t, struct gdbarch *);
-extern struct regcache *get_thread_arch_aspace_regcache
-  (inferior *inf_for_target_calls, ptid_t,
-   struct gdbarch *, struct address_space *);
+extern regcache *get_thread_arch_regcache (inferior *inf_for_target_calls,
+					   ptid_t ptid, gdbarch *arch);
 
 extern enum register_status
   regcache_raw_read_signed (struct regcache *regcache,
@@ -339,12 +335,6 @@ class regcache : public detached_regcache
 public:
   DISABLE_COPY_AND_ASSIGN (regcache);
 
-  /* Return REGCACHE's address space.  */
-  const address_space *aspace () const
-  {
-    return m_aspace;
-  }
-
   /* Restore 'this' regcache.  The set of registers restored into
      the regcache determined by the restore_reggroup.
      Writes to regcache will go through to the target.  SRC is a
@@ -422,8 +412,7 @@ class regcache : public detached_regcache
   void debug_print_register (const char *func, int regno);
 
 protected:
-  regcache (inferior *inf_for_target_calls, gdbarch *gdbarch,
-	    const address_space *aspace);
+  regcache (inferior *inf_for_target_calls, gdbarch *gdbarch);
 
 private:
 
@@ -445,10 +434,6 @@ class regcache : public detached_regcache
   enum register_status write_part (int regnum, int offset, int len,
 				   const gdb_byte *in, bool is_raw);
 
-  /* The address space of this register cache (for registers where it
-     makes sense, like PC or SP).  */
-  const address_space * const m_aspace;
-
   /* The inferior to switch to, to make target calls.
 
      This may not be the inferior of thread M_PTID.  For instance, this
@@ -462,10 +447,8 @@ class regcache : public detached_regcache
      it connected to?  */
   ptid_t m_ptid;
 
-  friend struct regcache *
-  get_thread_arch_aspace_regcache (inferior *inf_for_target_calls, ptid_t ptid,
-				   struct gdbarch *gdbarch,
-				   struct address_space *aspace);
+  friend regcache *get_thread_arch_regcache (inferior *inf_for_target_calls,
+					     ptid_t ptid, gdbarch *gdbarch);
 };
 
 using regcache_up = std::unique_ptr<regcache>;
diff --git a/gdb/remote.c b/gdb/remote.c
index 69271048da24..4c406d6aec56 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -8308,11 +8308,14 @@ remote_target::process_stop_reply (struct stop_reply *stop_reply,
       && status->kind () != TARGET_WAITKIND_SIGNALLED
       && status->kind () != TARGET_WAITKIND_NO_RESUMED)
     {
+      remote_notice_new_inferior (ptid, false);
+
       /* Expedited registers.  */
       if (!stop_reply->regcache.empty ())
 	{
-	  struct regcache *regcache
-	    = get_thread_arch_regcache (this, ptid, stop_reply->arch);
+	  regcache *regcache
+	    = get_thread_arch_regcache (find_inferior_ptid (this, ptid), ptid,
+					stop_reply->arch);
 
 	  for (cached_reg_t &reg : stop_reply->regcache)
 	    {
@@ -8323,7 +8326,6 @@ remote_target::process_stop_reply (struct stop_reply *stop_reply,
 	  stop_reply->regcache.clear ();
 	}
 
-      remote_notice_new_inferior (ptid, false);
       remote_thread_info *remote_thr = get_remote_thread_info (this, ptid);
       remote_thr->core = stop_reply->core;
       remote_thr->stop_reason = stop_reply->stop_reason;
diff --git a/gdb/riscv-fbsd-tdep.c b/gdb/riscv-fbsd-tdep.c
index 8ae4a1953bcc..9050cf425ec1 100644
--- a/gdb/riscv-fbsd-tdep.c
+++ b/gdb/riscv-fbsd-tdep.c
@@ -166,10 +166,8 @@ static CORE_ADDR
 riscv_fbsd_get_thread_local_address (struct gdbarch *gdbarch, ptid_t ptid,
 				     CORE_ADDR lm_addr, CORE_ADDR offset)
 {
-  struct regcache *regcache;
-
-  regcache = get_thread_arch_regcache (current_inferior ()->process_target (),
-				       ptid, gdbarch);
+  regcache *regcache
+    = get_thread_arch_regcache (current_inferior (), ptid, gdbarch);
 
   target_fetch_registers (regcache, RISCV_TP_REGNUM);
 
diff --git a/gdb/sol-thread.c b/gdb/sol-thread.c
index 537acdb57548..61331f9d40fc 100644
--- a/gdb/sol-thread.c
+++ b/gdb/sol-thread.c
@@ -845,9 +845,8 @@ ps_err_e
 ps_lgetregs (struct ps_prochandle *ph, lwpid_t lwpid, prgregset_t gregset)
 {
   ptid_t ptid = ptid_t (current_inferior ()->pid, lwpid, 0);
-  struct regcache *regcache
-    = get_thread_arch_regcache (current_inferior ()->process_target (),
-				ptid, current_inferior ()->arch ());
+  regcache *regcache = get_thread_arch_regcache (current_inferior (), ptid,
+						 current_inferior ()->arch ());
 
   target_fetch_registers (regcache, -1);
   fill_gregset (regcache, (gdb_gregset_t *) gregset, -1);
@@ -862,9 +861,8 @@ ps_lsetregs (struct ps_prochandle *ph, lwpid_t lwpid,
 	     const prgregset_t gregset)
 {
   ptid_t ptid = ptid_t (current_inferior ()->pid, lwpid, 0);
-  struct regcache *regcache
-    = get_thread_arch_regcache (current_inferior ()->process_target (),
-				ptid, current_inferior ()->arch ());
+  regcache *regcache = get_thread_arch_regcache (current_inferior (), ptid,
+						 current_inferior ()->arch ());
 
   supply_gregset (regcache, (const gdb_gregset_t *) gregset);
   target_store_registers (regcache, -1);
@@ -915,9 +913,8 @@ ps_lgetfpregs (struct ps_prochandle *ph, lwpid_t lwpid,
 	       prfpregset_t *fpregset)
 {
   ptid_t ptid = ptid_t (current_inferior ()->pid, lwpid, 0);
-  struct regcache *regcache
-    = get_thread_arch_regcache (current_inferior ()->process_target (),
-				ptid, current_inferior ()->arch ());
+  regcache *regcache = get_thread_arch_regcache (current_inferior (), ptid,
+						 current_inferior ()->arch ());
 
   target_fetch_registers (regcache, -1);
   fill_fpregset (regcache, (gdb_fpregset_t *) fpregset, -1);
@@ -932,9 +929,8 @@ ps_lsetfpregs (struct ps_prochandle *ph, lwpid_t lwpid,
 	       const prfpregset_t * fpregset)
 {
   ptid_t ptid = ptid_t (current_inferior ()->pid, lwpid, 0);
-  struct regcache *regcache
-    = get_thread_arch_regcache (current_inferior ()->process_target (),
-				ptid, current_inferior ()->arch ());
+  regcache *regcache = get_thread_arch_regcache (current_inferior (), ptid,
+						 current_inferior ()->arch ());
 
   supply_fpregset (regcache, (const gdb_fpregset_t *) fpregset);
   target_store_registers (regcache, -1);
diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c
index e21190a30f15..b02e8094ac6b 100644
--- a/gdb/solib-svr4.c
+++ b/gdb/solib-svr4.c
@@ -2399,9 +2399,8 @@ enable_break (struct svr4_info *info, int from_tty)
 	 most cases.  */
       if (!load_addr_found)
 	{
-	  struct regcache *regcache
-	    = get_thread_arch_regcache (current_inferior ()->process_target (),
-					inferior_ptid,
+	  regcache *regcache
+	    = get_thread_arch_regcache (current_inferior (), inferior_ptid,
 					current_inferior ()->arch ());
 
 	  load_addr = (regcache_read_pc (regcache)
diff --git a/gdb/target-delegates.c b/gdb/target-delegates.c
index 580fc768dd1c..f06249d49680 100644
--- a/gdb/target-delegates.c
+++ b/gdb/target-delegates.c
@@ -132,7 +132,6 @@ struct dummy_target : public target_ops
   void dumpcore (const char *arg0) override;
   bool can_run_breakpoint_commands () override;
   struct gdbarch *thread_architecture (ptid_t arg0) override;
-  struct address_space *thread_address_space (ptid_t arg0) override;
   bool filesystem_is_local () override;
   void trace_init () override;
   void download_tracepoint (struct bp_location *arg0) override;
@@ -307,7 +306,6 @@ struct debug_target : public target_ops
   void dumpcore (const char *arg0) override;
   bool can_run_breakpoint_commands () override;
   struct gdbarch *thread_architecture (ptid_t arg0) override;
-  struct address_space *thread_address_space (ptid_t arg0) override;
   bool filesystem_is_local () override;
   void trace_init () override;
   void download_tracepoint (struct bp_location *arg0) override;
@@ -2962,32 +2960,6 @@ debug_target::thread_architecture (ptid_t arg0)
   return result;
 }
 
-struct address_space *
-target_ops::thread_address_space (ptid_t arg0)
-{
-  return this->beneath ()->thread_address_space (arg0);
-}
-
-struct address_space *
-dummy_target::thread_address_space (ptid_t arg0)
-{
-  return NULL;
-}
-
-struct address_space *
-debug_target::thread_address_space (ptid_t arg0)
-{
-  gdb_printf (gdb_stdlog, "-> %s->thread_address_space (...)\n", this->beneath ()->shortname ());
-  struct address_space * result
-    = this->beneath ()->thread_address_space (arg0);
-  gdb_printf (gdb_stdlog, "<- %s->thread_address_space (", this->beneath ()->shortname ());
-  target_debug_print_ptid_t (arg0);
-  gdb_puts (") = ", gdb_stdlog);
-  target_debug_print_address_space_p (result);
-  gdb_puts ("\n", gdb_stdlog);
-  return result;
-}
-
 bool
 target_ops::filesystem_is_local ()
 {
diff --git a/gdb/target.c b/gdb/target.c
index f688ff33e3b0..cd648fd2e9ed 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -2992,19 +2992,6 @@ target_get_osdata (const char *type)
   return target_read_stralloc (t, TARGET_OBJECT_OSDATA, type);
 }
 
-/* Determine the current address space of thread PTID.  */
-
-struct address_space *
-target_thread_address_space (ptid_t ptid)
-{
-  struct address_space *aspace;
-
-  aspace = current_inferior ()->top_target ()->thread_address_space (ptid);
-  gdb_assert (aspace != NULL);
-
-  return aspace;
-}
-
 /* See target.h.  */
 
 target_ops *
diff --git a/gdb/target.h b/gdb/target.h
index 68b269fb3e61..94510b0ba22a 100644
--- a/gdb/target.h
+++ b/gdb/target.h
@@ -938,10 +938,6 @@ struct target_ops
     virtual struct gdbarch *thread_architecture (ptid_t)
       TARGET_DEFAULT_RETURN (NULL);
 
-    /* Determine current address space of thread PTID.  */
-    virtual struct address_space *thread_address_space (ptid_t)
-      TARGET_DEFAULT_RETURN (NULL);
-
     /* Target file operations.  */
 
     /* Return true if the filesystem seen by the current inferior
@@ -1533,10 +1529,6 @@ extern void target_store_registers (struct regcache *regcache, int regs);
 
 extern void target_prepare_to_store (regcache *regcache);
 
-/* Determine current address space of thread PTID.  */
-
-struct address_space *target_thread_address_space (ptid_t);
-
 /* Implement the "info proc" command.  This returns one if the request
    was handled, and zero otherwise.  It can also throw an exception if
    an error was encountered while attempting to handle the
-- 
2.42.0


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

* [PATCH 2/3] gdb: remove get_current_regcache
  2023-11-06 16:33 [PATCH 0/3] Three random cleanups Simon Marchi
  2023-11-06 16:33 ` [PATCH 1/3] gdb: remove regcache's address space Simon Marchi
@ 2023-11-06 16:33 ` Simon Marchi
  2023-11-06 16:33 ` [PATCH 3/3] gdb: pass address_space to target dcache functions Simon Marchi
  2023-11-17 14:33 ` [PATCH 0/3] Three random cleanups Tom Tromey
  3 siblings, 0 replies; 6+ messages in thread
From: Simon Marchi @ 2023-11-06 16:33 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

Remove get_current_regcache, inlining the call to get_thread_regcache in
callers.  When possible, pass the right thread_info object known from
the local context.  Otherwise, fall back to passing `inferior_thread ()`.

This makes the reference to global context bubble up one level, a small
step towards the long term goal of reducing the number of references to
global context (or rather, moving those references as close as possible
to the top of the call tree).

No behavior change expected.

Change-Id: Ifa6980c88825d803ea586546b6b4c633c33be8d6
---
 gdb/arc-tdep.c       |  6 +++---
 gdb/breakpoint.c     |  2 +-
 gdb/bsd-kvm.c        |  6 +++---
 gdb/corelow.c        |  2 +-
 gdb/frame.c          |  9 +++++----
 gdb/frv-tdep.c       |  2 +-
 gdb/i386-fbsd-nat.c  |  2 +-
 gdb/i386-tdep.c      |  3 +--
 gdb/ia64-linux-nat.c |  2 +-
 gdb/infcall.c        |  7 ++++---
 gdb/infcmd.c         |  5 +++--
 gdb/infrun.c         | 23 ++++++++++-------------
 gdb/linux-fork.c     | 10 ++++++----
 gdb/m32c-tdep.c      |  2 +-
 gdb/mep-tdep.c       |  4 ++--
 gdb/mi/mi-main.c     |  3 +--
 gdb/or1k-tdep.c      |  2 +-
 gdb/procfs.c         |  2 +-
 gdb/record-btrace.c  |  2 +-
 gdb/record-full.c    | 20 ++++++++++----------
 gdb/regcache-dump.c  |  5 +++--
 gdb/regcache.c       |  6 ------
 gdb/regcache.h       |  1 -
 gdb/reverse.c        |  7 ++++---
 gdb/solib-darwin.c   |  6 +++---
 gdb/solib-svr4.c     |  2 +-
 gdb/stack.c          |  6 +++---
 gdb/symfile.c        |  2 +-
 gdb/tracepoint.c     |  4 ++--
 gdb/windows-nat.c    |  4 ++--
 gdb/xtensa-tdep.c    |  6 ++++--
 31 files changed, 80 insertions(+), 83 deletions(-)

diff --git a/gdb/arc-tdep.c b/gdb/arc-tdep.c
index 0740eff02e45..a22b5bff246b 100644
--- a/gdb/arc-tdep.c
+++ b/gdb/arc-tdep.c
@@ -322,7 +322,7 @@ arc_insn_get_operand_value (const struct arc_instruction &insn,
       return insn.operands[operand_num].value;
     default:
       /* Value in instruction is a register number.  */
-      struct regcache *regcache = get_current_regcache ();
+      regcache *regcache = get_thread_regcache (inferior_thread ());
       ULONGEST value;
       regcache_cooked_read_unsigned (regcache,
 				     insn.operands[operand_num].value,
@@ -351,7 +351,7 @@ arc_insn_get_operand_value_signed (const struct arc_instruction &insn,
       return insn.operands[operand_num].value;
     default:
       /* Value in instruction is a register number.  */
-      struct regcache *regcache = get_current_regcache ();
+      regcache *regcache = get_thread_regcache (inferior_thread ());
       LONGEST value;
       regcache_cooked_read_signed (regcache,
 				   insn.operands[operand_num].value,
@@ -449,7 +449,7 @@ arc_insn_get_branch_target (const struct arc_instruction &insn)
   /* LEAVE_S: PC = BLINK.  */
   else if (insn.insn_class == LEAVE)
     {
-      struct regcache *regcache = get_current_regcache ();
+      regcache *regcache = get_thread_regcache (inferior_thread ());
       ULONGEST value;
       regcache_cooked_read_unsigned (regcache, ARC_BLINK_REGNUM, &value);
       return value;
diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index fe09dbcbeee3..4353cb2f9c07 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -13950,7 +13950,7 @@ insert_single_step_breakpoint (struct gdbarch *gdbarch,
 int
 insert_single_step_breakpoints (struct gdbarch *gdbarch)
 {
-  struct regcache *regcache = get_current_regcache ();
+  regcache *regcache = get_thread_regcache (inferior_thread ());
   std::vector<CORE_ADDR> next_pcs;
 
   next_pcs = gdbarch_software_single_step (gdbarch, regcache);
diff --git a/gdb/bsd-kvm.c b/gdb/bsd-kvm.c
index 0ed95f6246c9..bb2d038b2ce2 100644
--- a/gdb/bsd-kvm.c
+++ b/gdb/bsd-kvm.c
@@ -136,7 +136,7 @@ bsd_kvm_target_open (const char *arg, int from_tty)
   thread_info *thr = add_thread_silent (&bsd_kvm_ops, bsd_kvm_ptid);
   switch_to_thread (thr);
 
-  target_fetch_registers (get_current_regcache (), -1);
+  target_fetch_registers (get_thread_regcache (thr), -1);
 
   reinit_frame_cache ();
   print_stack_frame (get_selected_frame (NULL), 0, SRC_AND_LOC, 1);
@@ -334,7 +334,7 @@ bsd_kvm_proc_cmd (const char *arg, int fromtty)
   if (kvm_read (core_kd, addr, &bsd_kvm_paddr, sizeof bsd_kvm_paddr) == -1)
     error (("%s"), kvm_geterr (core_kd));
 
-  target_fetch_registers (get_current_regcache (), -1);
+  target_fetch_registers (get_thread_regcache (inferior_thread ()), -1);
 
   reinit_frame_cache ();
   print_stack_frame (get_selected_frame (NULL), 0, SRC_AND_LOC, 1);
@@ -354,7 +354,7 @@ bsd_kvm_pcb_cmd (const char *arg, int fromtty)
 
   bsd_kvm_paddr = (struct pcb *)(u_long) parse_and_eval_address (arg);
 
-  target_fetch_registers (get_current_regcache (), -1);
+  target_fetch_registers (get_thread_regcache (inferior_thread ()), -1);
 
   reinit_frame_cache ();
   print_stack_frame (get_selected_frame (NULL), 0, SRC_AND_LOC, 1);
diff --git a/gdb/corelow.c b/gdb/corelow.c
index 91b442b4426f..a38d9622f390 100644
--- a/gdb/corelow.c
+++ b/gdb/corelow.c
@@ -773,7 +773,7 @@ core_target_open (const char *arg, int from_tty)
     }
 
   /* Fetch all registers from core file.  */
-  target_fetch_registers (get_current_regcache (), -1);
+  target_fetch_registers (get_thread_regcache (inferior_thread ()), -1);
 
   /* Now, set up the frame cache, and print the top of stack.  */
   reinit_frame_cache ();
diff --git a/gdb/frame.c b/gdb/frame.c
index 2d63d6e4b4cc..261b99991156 100644
--- a/gdb/frame.c
+++ b/gdb/frame.c
@@ -1163,7 +1163,7 @@ frame_pop (frame_info_ptr this_frame)
      definition can lead to targets writing back bogus values
      (arguably a bug in the target code mind).  */
   /* Now copy those saved registers into the current regcache.  */
-  get_current_regcache ()->restore (scratch.get ());
+  get_thread_regcache (inferior_thread ())->restore (scratch.get ());
 
   /* We've made right mess of GDB's local state, just discard
      everything.  */
@@ -1444,7 +1444,7 @@ put_frame_register (frame_info_ptr frame, int regnum,
 	break;
       }
     case lval_register:
-      get_current_regcache ()->cooked_write (realnum, buf);
+      get_thread_regcache (inferior_thread ())->cooked_write (realnum, buf);
       break;
     default:
       error (_("Attempt to assign to an unmodifiable value."));
@@ -1689,7 +1689,8 @@ get_current_frame (void)
   if (sentinel_frame == NULL)
     sentinel_frame =
       create_sentinel_frame (current_program_space, current_inferior ()->aspace,
-			     get_current_regcache (), 0, 0).get ();
+			     get_thread_regcache (inferior_thread ()),
+			     0, 0).get ();
 
   /* Set the current frame before computing the frame id, to avoid
      recursion inside compute_frame_id, in case the frame's
@@ -2023,7 +2024,7 @@ create_new_frame (frame_id id)
 
   fi->next = create_sentinel_frame (current_program_space,
 				    current_inferior ()->aspace,
-				    get_current_regcache (),
+				    get_thread_regcache (inferior_thread ()),
 				    id.stack_addr, id.code_addr).get ();
 
   /* Set/update this frame's cached PC value, found in the next frame.
diff --git a/gdb/frv-tdep.c b/gdb/frv-tdep.c
index 3656f6fcb663..6528cf3e405e 100644
--- a/gdb/frv-tdep.c
+++ b/gdb/frv-tdep.c
@@ -114,7 +114,7 @@ frv_fdpic_loadmap_addresses (struct gdbarch *gdbarch, CORE_ADDR *interp_addr,
     return -1;
   else
     {
-      struct regcache *regcache = get_current_regcache ();
+      regcache *regcache = get_thread_regcache (inferior_thread ());
 
       if (interp_addr != NULL)
 	{
diff --git a/gdb/i386-fbsd-nat.c b/gdb/i386-fbsd-nat.c
index e2c0e6117171..7eb3b40e3695 100644
--- a/gdb/i386-fbsd-nat.c
+++ b/gdb/i386-fbsd-nat.c
@@ -233,7 +233,7 @@ i386_fbsd_nat_target::resume (ptid_t ptid, int step, enum gdb_signal signal)
 
   if (!step)
     {
-      struct regcache *regcache = get_current_regcache ();
+      regcache *regcache = get_thread_regcache (inferior_thread ());
       ULONGEST eflags;
 
       /* Workaround for a bug in FreeBSD.  Make sure that the trace
diff --git a/gdb/i386-tdep.c b/gdb/i386-tdep.c
index 1ddca2410fdf..f5ff55de47ac 100644
--- a/gdb/i386-tdep.c
+++ b/gdb/i386-tdep.c
@@ -8907,11 +8907,10 @@ i386_target_description (uint64_t xcr0, bool segments)
 static unsigned long
 i386_mpx_bd_base (void)
 {
-  struct regcache *rcache;
   ULONGEST ret;
   enum register_status regstatus;
 
-  rcache = get_current_regcache ();
+  regcache *rcache = get_thread_regcache (inferior_thread ());
   gdbarch *arch = rcache->arch ();
   i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (arch);
 
diff --git a/gdb/ia64-linux-nat.c b/gdb/ia64-linux-nat.c
index f9f21d8e067d..a610de60d446 100644
--- a/gdb/ia64-linux-nat.c
+++ b/gdb/ia64-linux-nat.c
@@ -693,7 +693,7 @@ ia64_linux_nat_target::stopped_data_address (CORE_ADDR *addr_p)
 {
   CORE_ADDR psr;
   siginfo_t siginfo;
-  struct regcache *regcache = get_current_regcache ();
+  regcache *regcache = get_thread_regcache (inferior_thread ());
 
   if (!linux_nat_get_siginfo (inferior_ptid, &siginfo))
     return false;
diff --git a/gdb/infcall.c b/gdb/infcall.c
index 0f9ad34bbb4e..322ea4b0f54d 100644
--- a/gdb/infcall.c
+++ b/gdb/infcall.c
@@ -482,7 +482,7 @@ get_call_return_value (struct call_return_meta_info *ri)
   else
     {
       gdbarch_return_value_as_value (ri->gdbarch, ri->function, ri->value_type,
-				     get_current_regcache (),
+				     get_thread_regcache (inferior_thread ()),
 				     &retval, NULL);
       if (stack_temporaries && class_or_union_p (ri->value_type))
 	{
@@ -1068,7 +1068,7 @@ call_function_by_hand_dummy (struct value *function,
 
 	sp = push_dummy_code (gdbarch, sp, funaddr, args,
 			      target_values_type, &real_pc, &bp_addr,
-			      get_current_regcache ());
+			      get_thread_regcache (inferior_thread ()));
 
 	/* Write a legitimate instruction at the point where the infcall
 	   breakpoint is going to be inserted.  While this instruction
@@ -1270,7 +1270,8 @@ call_function_by_hand_dummy (struct value *function,
   /* Create the dummy stack frame.  Pass in the call dummy address as,
      presumably, the ABI code knows where, in the call dummy, the
      return address should be pointed.  */
-  sp = gdbarch_push_dummy_call (gdbarch, function, get_current_regcache (),
+  sp = gdbarch_push_dummy_call (gdbarch, function,
+				get_thread_regcache (inferior_thread ()),
 				bp_addr, args.size (), args.data (),
 				sp, return_method, struct_addr);
 
diff --git a/gdb/infcmd.c b/gdb/infcmd.c
index cf8cd5279558..8890a16eade2 100644
--- a/gdb/infcmd.c
+++ b/gdb/infcmd.c
@@ -496,7 +496,8 @@ run_command_1 (const char *args, int from_tty, enum run_how run_how)
 
   /* Start the target running.  Do not use -1 continuation as it would skip
      breakpoint right at the entry point.  */
-  proceed (regcache_read_pc (get_current_regcache ()), GDB_SIGNAL_0);
+  proceed (regcache_read_pc (get_thread_regcache (inferior_thread ())),
+	   GDB_SIGNAL_0);
 
   /* Since there was no error, there's no need to finish the thread
      states here.  */
@@ -1466,7 +1467,7 @@ advance_command (const char *arg, int from_tty)
 struct value *
 get_return_value (struct symbol *func_symbol, struct value *function)
 {
-  regcache *stop_regs = get_current_regcache ();
+  regcache *stop_regs = get_thread_regcache (inferior_thread ());
   struct gdbarch *gdbarch = stop_regs->arch ();
   struct value *value;
 
diff --git a/gdb/infrun.c b/gdb/infrun.c
index 3a057764b149..d01c0ac11c12 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -2452,9 +2452,9 @@ do_target_resume (ptid_t resume_ptid, bool step, enum gdb_signal sig)
 static void
 resume_1 (enum gdb_signal sig)
 {
-  struct regcache *regcache = get_current_regcache ();
-  struct gdbarch *gdbarch = regcache->arch ();
   struct thread_info *tp = inferior_thread ();
+  regcache *regcache = get_thread_regcache (tp);
+  struct gdbarch *gdbarch = regcache->arch ();
   ptid_t resume_ptid;
   /* This represents the user's step vs continue request.  When
      deciding whether "set scheduler-locking step" applies, it's the
@@ -3397,7 +3397,6 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal)
 {
   INFRUN_SCOPED_DEBUG_ENTER_EXIT;
 
-  struct regcache *regcache;
   struct gdbarch *gdbarch;
   CORE_ADDR pc;
 
@@ -3418,15 +3417,13 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal)
   /* We'll update this if & when we switch to a new thread.  */
   update_previous_thread ();
 
-  regcache = get_current_regcache ();
-  gdbarch = regcache->arch ();
-
-  pc = regcache_read_pc_protected (regcache);
-
   thread_info *cur_thr = inferior_thread ();
-
   infrun_debug_printf ("cur_thr = %s", cur_thr->ptid.to_string ().c_str ());
 
+  regcache *regcache = get_thread_regcache (cur_thr);
+  gdbarch = regcache->arch ();
+  pc = regcache_read_pc_protected (regcache);
+
   /* Fill in with reasonable starting values.  */
   init_thread_stepping_state (cur_thr);
 
@@ -8490,7 +8487,7 @@ keep_going_pass_signal (struct execution_control_state *ecs)
     }
   else
     {
-      struct regcache *regcache = get_current_regcache ();
+      regcache *regcache = get_thread_regcache (ecs->event_thread);
       int remove_bp;
       int remove_wps;
       step_over_what step_what;
@@ -8708,7 +8705,7 @@ print_signal_received_reason (struct ui_out *uiout, enum gdb_signal siggnal)
       annotate_signal_string ();
       uiout->field_string ("signal-meaning", gdb_signal_to_string (siggnal));
 
-      struct regcache *regcache = get_current_regcache ();
+      regcache *regcache = get_thread_regcache (thr);
       struct gdbarch *gdbarch = regcache->arch ();
       if (gdbarch_report_signal_info_p (gdbarch))
 	gdbarch_report_signal_info (gdbarch, uiout, siggnal);
@@ -9611,7 +9608,7 @@ infcall_suspend_state_up
 save_infcall_suspend_state ()
 {
   struct thread_info *tp = inferior_thread ();
-  struct regcache *regcache = get_current_regcache ();
+  regcache *regcache = get_thread_regcache (tp);
   struct gdbarch *gdbarch = regcache->arch ();
 
   infcall_suspend_state_up inf_state
@@ -9632,7 +9629,7 @@ void
 restore_infcall_suspend_state (struct infcall_suspend_state *inf_state)
 {
   struct thread_info *tp = inferior_thread ();
-  struct regcache *regcache = get_current_regcache ();
+  regcache *regcache = get_thread_regcache (inferior_thread ());
   struct gdbarch *gdbarch = regcache->arch ();
 
   inf_state->restore (gdbarch, tp, regcache);
diff --git a/gdb/linux-fork.c b/gdb/linux-fork.c
index 52e385411c7e..39c3a6b34489 100644
--- a/gdb/linux-fork.c
+++ b/gdb/linux-fork.c
@@ -219,12 +219,13 @@ fork_load_infrun_state (struct fork_info *fp)
   linux_nat_switch_fork (fp->ptid);
 
   if (fp->savedregs)
-    get_current_regcache ()->restore (fp->savedregs);
+    get_thread_regcache (inferior_thread ())->restore (fp->savedregs);
 
   registers_changed ();
   reinit_frame_cache ();
 
-  inferior_thread ()->set_stop_pc (regcache_read_pc (get_current_regcache ()));
+  inferior_thread ()->set_stop_pc
+    (regcache_read_pc (get_thread_regcache (inferior_thread ())));
   nullify_last_target_wait_ptid ();
 
   /* Now restore the file positions of open file descriptors.  */
@@ -251,8 +252,9 @@ fork_save_infrun_state (struct fork_info *fp)
   if (fp->savedregs)
     delete fp->savedregs;
 
-  fp->savedregs = new readonly_detached_regcache (*get_current_regcache ());
-  fp->pc = regcache_read_pc (get_current_regcache ());
+  fp->savedregs = new readonly_detached_regcache
+    (*get_thread_regcache (inferior_thread ()));
+  fp->pc = regcache_read_pc (get_thread_regcache (inferior_thread ()));
 
   /* Now save the 'state' (file position) of all open file descriptors.
      Unfortunately fork does not take care of that for us...  */
diff --git a/gdb/m32c-tdep.c b/gdb/m32c-tdep.c
index e06dbbe12bff..a0f1779c41b0 100644
--- a/gdb/m32c-tdep.c
+++ b/gdb/m32c-tdep.c
@@ -2556,7 +2556,7 @@ m32c_virtual_frame_pointer (struct gdbarch *gdbarch, CORE_ADDR pc,
   CORE_ADDR func_addr, func_end;
   struct m32c_prologue p;
 
-  struct regcache *regcache = get_current_regcache ();
+  regcache *regcache = get_thread_regcache (inferior_thread ());
   m32c_gdbarch_tdep *tdep = gdbarch_tdep<m32c_gdbarch_tdep> (gdbarch);
   
   if (!find_pc_partial_function (pc, &name, &func_addr, &func_end))
diff --git a/gdb/mep-tdep.c b/gdb/mep-tdep.c
index a84df4e1f53c..6210b929f9d0 100644
--- a/gdb/mep-tdep.c
+++ b/gdb/mep-tdep.c
@@ -852,7 +852,7 @@ current_me_module (void)
   if (target_has_registers ())
     {
       ULONGEST regval;
-      regcache_cooked_read_unsigned (get_current_regcache (),
+      regcache_cooked_read_unsigned (get_thread_regcache (inferior_thread ()),
 				     MEP_MODULE_REGNUM, &regval);
       return (CONFIG_ATTR) regval;
     }
@@ -879,7 +879,7 @@ current_options (void)
   if (target_has_registers ())
     {
       ULONGEST regval;
-      regcache_cooked_read_unsigned (get_current_regcache (),
+      regcache_cooked_read_unsigned (get_thread_regcache (inferior_thread ()),
 				     MEP_OPT_REGNUM, &regval);
       return regval;
     }
diff --git a/gdb/mi/mi-main.c b/gdb/mi/mi-main.c
index 944c9116731f..abcd0d392ba1 100644
--- a/gdb/mi/mi-main.c
+++ b/gdb/mi/mi-main.c
@@ -1132,7 +1132,6 @@ void
 mi_cmd_data_write_register_values (const char *command,
 				   const char *const *argv, int argc)
 {
-  struct regcache *regcache;
   struct gdbarch *gdbarch;
   int numregs, i;
 
@@ -1143,7 +1142,7 @@ mi_cmd_data_write_register_values (const char *command,
      will change depending upon the particular processor being
      debugged.  */
 
-  regcache = get_current_regcache ();
+  regcache *regcache = get_thread_regcache (inferior_thread ());
   gdbarch = regcache->arch ();
   numregs = gdbarch_num_cooked_regs (gdbarch);
 
diff --git a/gdb/or1k-tdep.c b/gdb/or1k-tdep.c
index a472a766959c..74d56d9a8bd5 100644
--- a/gdb/or1k-tdep.c
+++ b/gdb/or1k-tdep.c
@@ -383,7 +383,7 @@ or1k_single_step_through_delay (struct gdbarch *gdbarch,
   ULONGEST val;
   CORE_ADDR ppc;
   CORE_ADDR npc;
-  struct regcache *regcache = get_current_regcache ();
+  regcache *regcache = get_thread_regcache (inferior_thread ());
 
   /* Get the previous and current instruction addresses.  If they are not
     adjacent, we cannot be in a delay slot.  */
diff --git a/gdb/procfs.c b/gdb/procfs.c
index 0e8a08f99d30..3a5d173ee429 100644
--- a/gdb/procfs.c
+++ b/gdb/procfs.c
@@ -3605,7 +3605,7 @@ procfs_target::make_corefile_notes (bfd *obfd, int *note_size)
 
   stop_signal = find_stop_signal ();
 
-  fill_gregset (get_current_regcache (), &gregs, -1);
+  fill_gregset (get_thread_regcache (inferior_thread ()), &gregs, -1);
   note_data.reset (elfcore_write_pstatus (obfd, note_data.release (), note_size,
 					  inferior_ptid.pid (),
 					  stop_signal, &gregs));
diff --git a/gdb/record-btrace.c b/gdb/record-btrace.c
index abab79f3132c..f0e190f9bf7a 100644
--- a/gdb/record-btrace.c
+++ b/gdb/record-btrace.c
@@ -2782,7 +2782,7 @@ record_btrace_set_replay (struct thread_info *tp,
   /* Start anew from the new replay position.  */
   record_btrace_clear_histories (btinfo);
 
-  inferior_thread ()->set_stop_pc (regcache_read_pc (get_current_regcache ()));
+  tp->set_stop_pc (regcache_read_pc (get_thread_regcache (tp)));
   print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
 }
 
diff --git a/gdb/record-full.c b/gdb/record-full.c
index 1cea869e88bb..0ce6862f819d 100644
--- a/gdb/record-full.c
+++ b/gdb/record-full.c
@@ -913,7 +913,7 @@ record_full_async_inferior_event_handler (gdb_client_data data)
 static void
 record_full_core_open_1 (const char *name, int from_tty)
 {
-  struct regcache *regcache = get_current_regcache ();
+  regcache *regcache = get_thread_regcache (inferior_thread ());
   int regnum = gdbarch_num_regs (regcache->arch ());
   int i;
 
@@ -1069,7 +1069,7 @@ record_full_target::resume (ptid_t ptid, int step, enum gdb_signal signal)
     {
       struct gdbarch *gdbarch = target_thread_architecture (ptid);
 
-      record_full_message (get_current_regcache (), signal);
+      record_full_message (get_thread_regcache (inferior_thread ()), signal);
 
       if (!step)
 	{
@@ -1216,7 +1216,7 @@ record_full_wait_1 (struct target_ops *ops,
 		  registers_changed ();
 		  switch_to_thread (current_inferior ()->process_target (),
 				    ret);
-		  regcache = get_current_regcache ();
+		  regcache = get_thread_regcache (inferior_thread ());
 		  tmp_pc = regcache_read_pc (regcache);
 		  const address_space *aspace = current_inferior ()->aspace;
 
@@ -1286,7 +1286,7 @@ record_full_wait_1 (struct target_ops *ops,
     {
       switch_to_thread (current_inferior ()->process_target (),
 			record_full_resume_ptid);
-      struct regcache *regcache = get_current_regcache ();
+      regcache *regcache = get_thread_regcache (inferior_thread ());
       struct gdbarch *gdbarch = regcache->arch ();
       const address_space *aspace = current_inferior ()->aspace;
       int continue_flag = 1;
@@ -2001,7 +2001,9 @@ record_full_goto_entry (struct record_full_entry *p)
 
   registers_changed ();
   reinit_frame_cache ();
-  inferior_thread ()->set_stop_pc (regcache_read_pc (get_current_regcache ()));
+  
+  thread_info *thr = inferior_thread ();
+  thr->set_stop_pc (regcache_read_pc (get_thread_regcache (thr)));
   print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
 }
 
@@ -2322,7 +2324,6 @@ record_full_restore (void)
   asection *osec;
   uint32_t osec_size;
   int bfd_offset = 0;
-  struct regcache *regcache;
 
   /* We restore the execution log from the open core bfd,
      if there is one.  */
@@ -2365,7 +2366,7 @@ record_full_restore (void)
 
   try
     {
-      regcache = get_current_regcache ();
+      regcache *regcache = get_thread_regcache (inferior_thread ());
 
       while (1)
 	{
@@ -2525,7 +2526,6 @@ record_full_base_target::save_record (const char *recfilename)
 {
   struct record_full_entry *cur_record_full_list;
   uint32_t magic;
-  struct regcache *regcache;
   struct gdbarch *gdbarch;
   int save_size = 0;
   asection *osec = NULL;
@@ -2546,7 +2546,7 @@ record_full_base_target::save_record (const char *recfilename)
   cur_record_full_list = record_full_list;
 
   /* Get the values of regcache and gdbarch.  */
-  regcache = get_current_regcache ();
+  regcache *regcache = get_thread_regcache (inferior_thread ());
   gdbarch = regcache->arch ();
 
   /* Disable the GDB operation record.  */
@@ -2731,7 +2731,7 @@ record_full_goto_insn (struct record_full_entry *entry,
 {
   scoped_restore restore_operation_disable
     = record_full_gdb_operation_disable_set ();
-  struct regcache *regcache = get_current_regcache ();
+  regcache *regcache = get_thread_regcache (inferior_thread ());
   struct gdbarch *gdbarch = regcache->arch ();
 
   /* Assume everything is valid: we will hit the entry,
diff --git a/gdb/regcache-dump.c b/gdb/regcache-dump.c
index 585e02eefed5..4b9be859157f 100644
--- a/gdb/regcache-dump.c
+++ b/gdb/regcache-dump.c
@@ -233,7 +233,7 @@ regcache_print (const char *args, enum regcache_dump_what what_to_dump)
   gdbarch *gdbarch;
 
   if (target_has_registers ())
-    gdbarch = get_current_regcache ()->arch ();
+    gdbarch = get_thread_regcache (inferior_thread ())->arch ();
   else
     gdbarch = current_inferior ()->arch ();
 
@@ -254,7 +254,8 @@ regcache_print (const char *args, enum regcache_dump_what what_to_dump)
 	auto dump_pseudo = (what_to_dump == regcache_dump_cooked);
 
 	if (target_has_registers ())
-	  dump.reset (new register_dump_regcache (get_current_regcache (),
+	  dump.reset (new register_dump_regcache (get_thread_regcache
+						    (inferior_thread ()),
 						  dump_pseudo));
 	else
 	  {
diff --git a/gdb/regcache.c b/gdb/regcache.c
index 48a926ed4483..48d7db47c06e 100644
--- a/gdb/regcache.c
+++ b/gdb/regcache.c
@@ -415,12 +415,6 @@ get_thread_regcache (thread_info *thread)
 			      thread->ptid);
 }
 
-struct regcache *
-get_current_regcache (void)
-{
-  return get_thread_regcache (inferior_thread ());
-}
-
 /* See gdbsupport/common-regcache.h.  */
 
 struct regcache *
diff --git a/gdb/regcache.h b/gdb/regcache.h
index f73f08a44ab7..7922a5c5ae21 100644
--- a/gdb/regcache.h
+++ b/gdb/regcache.h
@@ -30,7 +30,6 @@ class thread_info;
 struct process_stratum_target;
 struct inferior;
 
-extern struct regcache *get_current_regcache (void);
 extern struct regcache *get_thread_regcache (process_stratum_target *target,
 					     ptid_t ptid);
 
diff --git a/gdb/reverse.c b/gdb/reverse.c
index 0a9cc298aeb9..30d621f25a0d 100644
--- a/gdb/reverse.c
+++ b/gdb/reverse.c
@@ -113,7 +113,8 @@ save_bookmark_command (const char *args, int from_tty)
 {
   /* Get target's idea of a bookmark.  */
   gdb_byte *bookmark_id = target_get_bookmark (args, from_tty);
-  struct gdbarch *gdbarch = get_current_regcache ()->arch ();
+  regcache *regcache = get_thread_regcache (inferior_thread ());
+  gdbarch *gdbarch = regcache->arch ();
 
   /* CR should not cause another identical bookmark.  */
   dont_repeat ();
@@ -125,7 +126,7 @@ save_bookmark_command (const char *args, int from_tty)
   all_bookmarks.emplace_back ();
   bookmark &b = all_bookmarks.back ();
   b.number = ++bookmark_count;
-  b.pc = regcache_read_pc (get_current_regcache ());
+  b.pc = regcache_read_pc (regcache);
   b.sal = find_pc_line (b.pc, 0);
   b.sal.pspace = get_frame_program_space (get_current_frame ());
   b.opaque_data.reset (bookmark_id);
@@ -237,7 +238,7 @@ goto_bookmark_command (const char *args, int from_tty)
 static int
 bookmark_1 (int bnum)
 {
-  struct gdbarch *gdbarch = get_current_regcache ()->arch ();
+  gdbarch *gdbarch = get_thread_regcache (inferior_thread ())->arch ();
   int matched = 0;
 
   for (const bookmark &iter : all_bookmarks)
diff --git a/gdb/solib-darwin.c b/gdb/solib-darwin.c
index 4f9ba9945ce0..c18791cd7c2c 100644
--- a/gdb/solib-darwin.c
+++ b/gdb/solib-darwin.c
@@ -362,7 +362,7 @@ darwin_read_exec_load_addr_at_init (struct darwin_info *info)
   gdb_byte buf[8];
 
   /* Get SP.  */
-  if (regcache_cooked_read_unsigned (get_current_regcache (),
+  if (regcache_cooked_read_unsigned (get_thread_regcache (inferior_thread ()),
 				     gdbarch_sp_regnum (gdbarch),
 				     &load_ptr_addr) != REG_VALID)
     return 0;
@@ -450,7 +450,7 @@ darwin_solib_get_all_image_info_addr_at_init (struct darwin_info *info)
   /* We find the dynamic linker's base address by examining
      the current pc (which should point at the entry point for the
      dynamic linker) and subtracting the offset of the entry point.  */
-  load_addr = (regcache_read_pc (get_current_regcache ())
+  load_addr = (regcache_read_pc (get_thread_regcache (inferior_thread ()))
 	       - bfd_get_start_address (dyld_bfd.get ()));
 
   /* Now try to set a breakpoint in the dynamic linker.  */
@@ -574,7 +574,7 @@ darwin_solib_create_inferior_hook (int from_tty)
 		 for the dynamic linker) and subtracting the offset of
 		 the entry point.  */
 
-	      pc = regcache_read_pc (get_current_regcache ());
+	      pc = regcache_read_pc (get_thread_regcache (inferior_thread ()));
 	      dyld_relocated_base_address = pc - dyld_bfd_start_address;
 
 	      /* We get the proper notifier relocated address by
diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c
index b02e8094ac6b..8a5ad590f6f9 100644
--- a/gdb/solib-svr4.c
+++ b/gdb/solib-svr4.c
@@ -1867,7 +1867,7 @@ svr4_handle_solib_event (void)
   if (info->probes_table == NULL)
     return;
 
-  pc = regcache_read_pc (get_current_regcache ());
+  pc = regcache_read_pc (get_thread_regcache (inferior_thread ()));
   pa = solib_event_probe_at (info, pc);
   if (pa == nullptr)
     {
diff --git a/gdb/stack.c b/gdb/stack.c
index 39dcefa87b88..ef565445c164 100644
--- a/gdb/stack.c
+++ b/gdb/stack.c
@@ -2798,13 +2798,13 @@ return_command (const char *retval_exp, int from_tty)
   if (return_value != NULL)
     {
       struct type *return_type = return_value->type ();
-      struct gdbarch *cache_arch = get_current_regcache ()->arch ();
+      regcache *regcache = get_thread_regcache (inferior_thread ());
+      struct gdbarch *cache_arch = regcache->arch ();
 
       gdb_assert (rv_conv != RETURN_VALUE_STRUCT_CONVENTION
 		  && rv_conv != RETURN_VALUE_ABI_RETURNS_ADDRESS);
       gdbarch_return_value_as_value
-	(cache_arch, function, return_type,
-	 get_current_regcache (), NULL /*read*/,
+	(cache_arch, function, return_type, regcache, NULL /*read*/,
 	 return_value->contents ().data () /*write*/);
     }
 
diff --git a/gdb/symfile.c b/gdb/symfile.c
index eebc5ea44b9b..7f7ac3378061 100644
--- a/gdb/symfile.c
+++ b/gdb/symfile.c
@@ -2098,7 +2098,7 @@ generic_load (const char *args, int from_tty)
   uiout->text (", load size ");
   uiout->field_unsigned ("load-size", total_progress.data_count);
   uiout->text ("\n");
-  regcache_write_pc (get_current_regcache (), entry);
+  regcache_write_pc (get_thread_regcache (inferior_thread ()), entry);
 
   /* Reset breakpoints, now that we have changed the load image.  For
      instance, breakpoints may have been set (or reset, by
diff --git a/gdb/tracepoint.c b/gdb/tracepoint.c
index 2c5486885700..d5f29eb8b108 100644
--- a/gdb/tracepoint.c
+++ b/gdb/tracepoint.c
@@ -2283,7 +2283,7 @@ tfind_pc_command (const char *args, int from_tty)
   check_trace_running (current_trace_status ());
 
   if (args == 0 || *args == 0)
-    pc = regcache_read_pc (get_current_regcache ());
+    pc = regcache_read_pc (get_thread_regcache (inferior_thread ()));
   else
     pc = parse_and_eval_address (args);
 
@@ -2731,7 +2731,7 @@ get_traceframe_location (int *stepping_frame_p)
   /* The current frame is a trap frame if the frame PC is equal to the
      tracepoint PC.  If not, then the current frame was collected
      during single-stepping.  */
-  regcache = get_current_regcache ();
+  regcache = get_thread_regcache (inferior_thread ());
 
   /* If the traceframe's address matches any of the tracepoint's
      locations, assume it is a direct hit rather than a while-stepping
diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
index b446afd72d8a..322ed051ce27 100644
--- a/gdb/windows-nat.c
+++ b/gdb/windows-nat.c
@@ -1441,7 +1441,7 @@ windows_nat_target::resume (ptid_t ptid, int step, enum gdb_signal sig)
 	  if (step)
 	    {
 	      /* Single step by setting t bit.  */
-	      struct regcache *regcache = get_current_regcache ();
+	      regcache *regcache = get_thread_regcache (inferior_thread ());
 	      struct gdbarch *gdbarch = regcache->arch ();
 	      fetch_registers (regcache, gdbarch_ps_regnum (gdbarch));
 	      th->wow64_context.EFlags |= FLAG_TRACE_BIT;
@@ -1469,7 +1469,7 @@ windows_nat_target::resume (ptid_t ptid, int step, enum gdb_signal sig)
 	  if (step)
 	    {
 	      /* Single step by setting t bit.  */
-	      struct regcache *regcache = get_current_regcache ();
+	      regcache *regcache = get_thread_regcache (inferior_thread ());
 	      struct gdbarch *gdbarch = regcache->arch ();
 	      fetch_registers (regcache, gdbarch_ps_regnum (gdbarch));
 	      th->context.EFlags |= FLAG_TRACE_BIT;
diff --git a/gdb/xtensa-tdep.c b/gdb/xtensa-tdep.c
index c9935d152641..9e7bfde193fc 100644
--- a/gdb/xtensa-tdep.c
+++ b/gdb/xtensa-tdep.c
@@ -161,7 +161,8 @@ xtensa_read_register (int regnum)
 {
   ULONGEST value;
 
-  regcache_raw_read_unsigned (get_current_regcache (), regnum, &value);
+  regcache_raw_read_unsigned (get_thread_regcache (inferior_thread ()), regnum,
+			      &value);
   return (unsigned long) value;
 }
 
@@ -169,7 +170,8 @@ xtensa_read_register (int regnum)
 static void
 xtensa_write_register (int regnum, ULONGEST value)
 {
-  regcache_raw_write_unsigned (get_current_regcache (), regnum, value);
+  regcache_raw_write_unsigned (get_thread_regcache (inferior_thread ()), regnum,
+			       value);
 }
 
 /* Return the window size of the previous call to the function from which we
-- 
2.42.0


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

* [PATCH 3/3] gdb: pass address_space to target dcache functions
  2023-11-06 16:33 [PATCH 0/3] Three random cleanups Simon Marchi
  2023-11-06 16:33 ` [PATCH 1/3] gdb: remove regcache's address space Simon Marchi
  2023-11-06 16:33 ` [PATCH 2/3] gdb: remove get_current_regcache Simon Marchi
@ 2023-11-06 16:33 ` Simon Marchi
  2023-11-17 14:33 ` [PATCH 0/3] Three random cleanups Tom Tromey
  3 siblings, 0 replies; 6+ messages in thread
From: Simon Marchi @ 2023-11-06 16:33 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

A simple refactor to make the reference to current_program_space bubble
up one level.  No behavior changes expected.

Change-Id: I237cf2f45ae73c35bcb433ce40e3c03cef6b87e2
---
 gdb/dcache.c        |  6 +++---
 gdb/infrun.c        |  6 +++---
 gdb/memattr.c       |  7 ++++---
 gdb/target-dcache.c | 24 ++++++++++++------------
 gdb/target-dcache.h | 10 ++++++----
 gdb/target.c        | 11 ++++++-----
 gdb/top.c           |  2 +-
 gdb/tracepoint.c    |  2 +-
 8 files changed, 36 insertions(+), 32 deletions(-)

diff --git a/gdb/dcache.c b/gdb/dcache.c
index 8a5fb13acf5c..229c13f8b5f7 100644
--- a/gdb/dcache.c
+++ b/gdb/dcache.c
@@ -651,7 +651,7 @@ dcache_info_1 (DCACHE *dcache, const char *exp)
 static void
 info_dcache_command (const char *exp, int tty)
 {
-  dcache_info_1 (target_dcache_get (), exp);
+  dcache_info_1 (target_dcache_get (current_program_space->aspace), exp);
 }
 
 static void
@@ -663,7 +663,7 @@ set_dcache_size (const char *args, int from_tty,
       dcache_size = DCACHE_DEFAULT_SIZE;
       error (_("Dcache size must be greater than 0."));
     }
-  target_dcache_invalidate ();
+  target_dcache_invalidate (current_program_space->aspace);
 }
 
 static void
@@ -677,7 +677,7 @@ set_dcache_line_size (const char *args, int from_tty,
       dcache_line_size = DCACHE_DEFAULT_LINE_SIZE;
       error (_("Invalid dcache line size: %u (must be power of 2)."), d);
     }
-  target_dcache_invalidate ();
+  target_dcache_invalidate (current_program_space->aspace);
 }
 
 void _initialize_dcache ();
diff --git a/gdb/infrun.c b/gdb/infrun.c
index d01c0ac11c12..e6af73543d65 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -4186,7 +4186,7 @@ wait_for_inferior (inferior *inf)
 	 Target was running and cache could be stale.  This is just a
 	 heuristic.  Running threads may modify target memory, but we
 	 don't get any event.  */
-      target_dcache_invalidate ();
+      target_dcache_invalidate (current_program_space->aspace);
 
       ecs.ptid = do_target_wait_1 (inf, minus_one_ptid, &ecs.ws, 0);
       ecs.target = inf->process_target ();
@@ -4416,7 +4416,7 @@ fetch_inferior_event ()
        was running and cache could be stale.  This is just a heuristic.
        Running threads may modify target memory, but we don't get any
        event.  */
-    target_dcache_invalidate ();
+    target_dcache_invalidate (current_program_space->aspace);
 
     scoped_restore save_exec_dir
       = make_scoped_restore (&execution_direction,
@@ -4995,7 +4995,7 @@ poll_one_curr_target (struct target_waitstatus *ws)
      Target was running and cache could be stale.  This is just a
      heuristic.  Running threads may modify target memory, but we
      don't get any event.  */
-  target_dcache_invalidate ();
+  target_dcache_invalidate (current_program_space->aspace);
 
   event_ptid = target_wait (minus_one_ptid, ws, TARGET_WNOHANG);
 
diff --git a/gdb/memattr.c b/gdb/memattr.c
index 8b0e3ef7b7ea..44ccd9f6cd10 100644
--- a/gdb/memattr.c
+++ b/gdb/memattr.c
@@ -30,6 +30,7 @@
 #include <algorithm>
 #include "gdbarch.h"
 #include "inferior.h"
+#include "progspace.h"
 
 static std::vector<mem_region> user_mem_region_list, target_mem_region_list;
 static std::vector<mem_region> *mem_region_list = &target_mem_region_list;
@@ -483,7 +484,7 @@ enable_mem_command (const char *args, int from_tty)
 {
   require_user_regions (from_tty);
 
-  target_dcache_invalidate ();
+  target_dcache_invalidate (current_program_space->aspace);
 
   if (args == NULL || *args == '\0')
     { /* Enable all mem regions.  */
@@ -521,7 +522,7 @@ disable_mem_command (const char *args, int from_tty)
 {
   require_user_regions (from_tty);
 
-  target_dcache_invalidate ();
+  target_dcache_invalidate (current_program_space->aspace);
 
   if (args == NULL || *args == '\0')
     {
@@ -567,7 +568,7 @@ delete_mem_command (const char *args, int from_tty)
 {
   require_user_regions (from_tty);
 
-  target_dcache_invalidate ();
+  target_dcache_invalidate (current_program_space->aspace);
 
   if (args == NULL || *args == '\0')
     {
diff --git a/gdb/target-dcache.c b/gdb/target-dcache.c
index 13c2888e7ea8..4c4fef902c4a 100644
--- a/gdb/target-dcache.c
+++ b/gdb/target-dcache.c
@@ -30,10 +30,10 @@ static const registry<address_space>::key<DCACHE, dcache_deleter>
 /* Target dcache is initialized or not.  */
 
 int
-target_dcache_init_p (void)
+target_dcache_init_p (address_space *aspace)
 {
   DCACHE *dcache
-    = target_dcache_aspace_key.get (current_program_space->aspace);
+    = target_dcache_aspace_key.get (aspace);
 
   return (dcache != NULL);
 }
@@ -41,10 +41,10 @@ target_dcache_init_p (void)
 /* Invalidate the target dcache.  */
 
 void
-target_dcache_invalidate (void)
+target_dcache_invalidate (address_space *aspace)
 {
   DCACHE *dcache
-    = target_dcache_aspace_key.get (current_program_space->aspace);
+    = target_dcache_aspace_key.get (aspace);
 
   if (dcache != NULL)
     dcache_invalidate (dcache);
@@ -54,24 +54,24 @@ target_dcache_invalidate (void)
    initialized yet.  */
 
 DCACHE *
-target_dcache_get (void)
+target_dcache_get (address_space *aspace)
 {
-  return target_dcache_aspace_key.get (current_program_space->aspace);
+  return target_dcache_aspace_key.get (aspace);
 }
 
 /* Return the target dcache.  If it is not initialized yet, initialize
    it.  */
 
 DCACHE *
-target_dcache_get_or_init (void)
+target_dcache_get_or_init (address_space *aspace)
 {
   DCACHE *dcache
-    = target_dcache_aspace_key.get (current_program_space->aspace);
+    = target_dcache_aspace_key.get (aspace);
 
   if (dcache == NULL)
     {
       dcache = dcache_init ();
-      target_dcache_aspace_key.set (current_program_space->aspace, dcache);
+      target_dcache_aspace_key.set (aspace, dcache);
     }
 
   return dcache;
@@ -93,7 +93,7 @@ static void
 set_stack_cache (const char *args, int from_tty, struct cmd_list_element *c)
 {
   if (stack_cache_enabled != stack_cache_enabled_1)
-    target_dcache_invalidate ();
+    target_dcache_invalidate (current_program_space->aspace);
 
   stack_cache_enabled = stack_cache_enabled_1;
 }
@@ -131,7 +131,7 @@ static void
 set_code_cache (const char *args, int from_tty, struct cmd_list_element *c)
 {
   if (code_cache_enabled != code_cache_enabled_1)
-    target_dcache_invalidate ();
+    target_dcache_invalidate (current_program_space->aspace);
 
   code_cache_enabled = code_cache_enabled_1;
 }
@@ -158,7 +158,7 @@ code_cache_enabled_p (void)
 static void
 maint_flush_dcache_command (const char *command, int from_tty)
 {
-  target_dcache_invalidate ();
+  target_dcache_invalidate (current_program_space->aspace);
   if (from_tty)
     gdb_printf (_("The dcache was flushed.\n"));
 }
diff --git a/gdb/target-dcache.h b/gdb/target-dcache.h
index 1e9e9725f950..4693b388110e 100644
--- a/gdb/target-dcache.h
+++ b/gdb/target-dcache.h
@@ -20,13 +20,15 @@
 
 #include "dcache.h"
 
-extern void target_dcache_invalidate (void);
+struct address_space;
 
-extern DCACHE *target_dcache_get (void);
+extern void target_dcache_invalidate (address_space *aspace);
 
-extern DCACHE *target_dcache_get_or_init (void);
+extern DCACHE *target_dcache_get (address_space *aspace);
 
-extern int target_dcache_init_p (void);
+extern DCACHE *target_dcache_get_or_init (address_space *aspace);
+
+extern int target_dcache_init_p (address_space *aspace);
 
 extern int stack_cache_enabled_p (void);
 
diff --git a/gdb/target.c b/gdb/target.c
index cd648fd2e9ed..fae4e973f28b 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -893,7 +893,7 @@ target_kill (void)
 void
 target_load (const char *arg, int from_tty)
 {
-  target_dcache_invalidate ();
+  target_dcache_invalidate (current_program_space->aspace);
   current_inferior ()->top_target ()->load (arg, from_tty);
 }
 
@@ -1473,10 +1473,10 @@ raw_memory_xfer_partial (struct target_ops *ops, gdb_byte *readbuf,
      that never made it to the target.  */
   if (writebuf != NULL
       && inferior_ptid != null_ptid
-      && target_dcache_init_p ()
+      && target_dcache_init_p (current_program_space->aspace)
       && (stack_cache_enabled_p () || code_cache_enabled_p ()))
     {
-      DCACHE *dcache = target_dcache_get ();
+      DCACHE *dcache = target_dcache_get (current_program_space->aspace);
 
       /* Note that writing to an area of memory which wasn't present
 	 in the cache doesn't cause it to be loaded in.  */
@@ -1559,7 +1559,8 @@ memory_xfer_partial_1 (struct target_ops *ops, enum target_object object,
 	  || (stack_cache_enabled_p () && object == TARGET_OBJECT_STACK_MEMORY)
 	  || (code_cache_enabled_p () && object == TARGET_OBJECT_CODE_MEMORY)))
     {
-      DCACHE *dcache = target_dcache_get_or_init ();
+      DCACHE *dcache
+	= target_dcache_get_or_init (current_program_space->aspace);
 
       return dcache_read_memory_partial (ops, dcache, memaddr, readbuf,
 					 reg_len, xfered_len);
@@ -2632,7 +2633,7 @@ target_resume (ptid_t scope_ptid, int step, enum gdb_signal signal)
   gdb_assert (inferior_ptid != null_ptid);
   gdb_assert (inferior_ptid.matches (scope_ptid));
 
-  target_dcache_invalidate ();
+  target_dcache_invalidate (current_program_space->aspace);
 
   current_inferior ()->top_target ()->resume (scope_ptid, step, signal);
 
diff --git a/gdb/top.c b/gdb/top.c
index 5028440b6711..d211f1b08be8 100644
--- a/gdb/top.c
+++ b/gdb/top.c
@@ -360,7 +360,7 @@ prepare_execute_command ()
      it.  For the duration of the command, though, use the dcache to
      help things like backtrace.  */
   if (non_stop)
-    target_dcache_invalidate ();
+    target_dcache_invalidate (current_program_space->aspace);
 
   return scoped_value_mark ();
 }
diff --git a/gdb/tracepoint.c b/gdb/tracepoint.c
index d5f29eb8b108..750185341ea3 100644
--- a/gdb/tracepoint.c
+++ b/gdb/tracepoint.c
@@ -2131,7 +2131,7 @@ tfind_1 (enum trace_find_type type, int num,
   tp = get_tracepoint_by_number_on_target (target_tracept);
 
   reinit_frame_cache ();
-  target_dcache_invalidate ();
+  target_dcache_invalidate (current_program_space->aspace);
 
   set_tracepoint_num (tp ? tp->number : target_tracept);
 
-- 
2.42.0


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

* Re: [PATCH 0/3] Three random cleanups
  2023-11-06 16:33 [PATCH 0/3] Three random cleanups Simon Marchi
                   ` (2 preceding siblings ...)
  2023-11-06 16:33 ` [PATCH 3/3] gdb: pass address_space to target dcache functions Simon Marchi
@ 2023-11-17 14:33 ` Tom Tromey
  2023-11-18 20:36   ` Simon Marchi
  3 siblings, 1 reply; 6+ messages in thread
From: Tom Tromey @ 2023-11-17 14:33 UTC (permalink / raw)
  To: Simon Marchi; +Cc: gdb-patches

>>>>> "Simon" == Simon Marchi <simon.marchi@efficios.com> writes:

Simon> I have been working no a patch to remove program_space::aspace, which I
Simon> have reminded about by reading this series:

Simon>   https://inbox.sourceware.org/gdb-patches/20231104155757.16649-1-tdevries@suse.de/T/#t

Simon> These 3 patches are preparatory patches I made for my "remove
Simon> program_space::aspace" patch, but I think they are good on their own,
Simon> so rather than delay posting them, here they are now.

I skimmed these.  I agree with the rationales you posted and FWIW I
think they make sense to do.

Tom

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

* Re: [PATCH 0/3] Three random cleanups
  2023-11-17 14:33 ` [PATCH 0/3] Three random cleanups Tom Tromey
@ 2023-11-18 20:36   ` Simon Marchi
  0 siblings, 0 replies; 6+ messages in thread
From: Simon Marchi @ 2023-11-18 20:36 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

On 2023-11-17 09:33, Tom Tromey wrote:
>>>>>> "Simon" == Simon Marchi <simon.marchi@efficios.com> writes:
> 
> Simon> I have been working no a patch to remove program_space::aspace, which I
> Simon> have reminded about by reading this series:
> 
> Simon>   https://inbox.sourceware.org/gdb-patches/20231104155757.16649-1-tdevries@suse.de/T/#t
> 
> Simon> These 3 patches are preparatory patches I made for my "remove
> Simon> program_space::aspace" patch, but I think they are good on their own,
> Simon> so rather than delay posting them, here they are now.
> 
> I skimmed these.  I agree with the rationales you posted and FWIW I
> think they make sense to do.

I forgot to mention it, but I pushed this yesterday.  I also meant to
translate your reply into some "Acked-By"s, but I forgot :(.

Simon

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

end of thread, other threads:[~2023-11-18 20:36 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-06 16:33 [PATCH 0/3] Three random cleanups Simon Marchi
2023-11-06 16:33 ` [PATCH 1/3] gdb: remove regcache's address space Simon Marchi
2023-11-06 16:33 ` [PATCH 2/3] gdb: remove get_current_regcache Simon Marchi
2023-11-06 16:33 ` [PATCH 3/3] gdb: pass address_space to target dcache functions Simon Marchi
2023-11-17 14:33 ` [PATCH 0/3] Three random cleanups Tom Tromey
2023-11-18 20:36   ` Simon Marchi

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