public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 00/12] Concurrent displaced stepping
@ 2020-11-10 21:46 Simon Marchi
  2020-11-10 21:46 ` [PATCH 01/12] gdb: add inferior_execd observable Simon Marchi
                   ` (11 more replies)
  0 siblings, 12 replies; 44+ messages in thread
From: Simon Marchi @ 2020-11-10 21:46 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

This patch series changes GDB to allow concurrent displaced steps to
occur simultanesously in a single inferior.  The goal is to reduce the
bottleneck in applications that need to do many displaced steps at the
same time.

Patches 09, 11 and 12 are the main ones implementing the feature.  The
other ones are mostly preparatory patches to reduce the diff and
complexity in the main ones.

At the end of the series, GDB uses two displaced stepping buffers when
debugging an AMD64/Linux program, so is able to execute two displaced
steps at a time.  There isn't a super significant performance
improvement as a result.  However, this work was done in the context of
ROCm GDB [1], where we have as many displaced step buffers as there are
threads.  In this context, it makes a huge difference.  So, the feature
isn't absolutely necessary on AMD64/Linux, but it is there at least to
have one reference implementation of an architecture in upstream using
multiple displaced step buffers, so that it's possible to test the
feature with common hardware.

This was regtested on x86-64 Ubuntu 20.04 using the unix,
native-gdbserver and native-extended-gdbserver boards.

[1] https://github.com/ROCm-Developer-Tools/ROCgdb

Simon Marchi (12):
  gdb: add inferior_execd observable
  gdb: clear inferior displaced stepping state on exec
  gdb: rename things related to step over chains
  gdb: rename displaced_step_closure to displaced_step_copy_insn_closure
  gdb: rename displaced_step_fixup to displaced_step_finish
  gdb: introduce status enum for displaced step prepare/finish
  gdb: pass inferior to get_linux_inferior_data
  gdb: move displaced stepping types to displaced-stepping.{h,c}
  gdb: move displaced stepping logic to gdbarch, allow starting
    concurrent displaced steps
  gdb: change linux gdbarch data from post to pre-init
  gdb: make displaced stepping implementation capable of managing
    multiple buffers
  gdb: use two displaced step buffers on amd64/Linux

 gdb/Makefile.in                               |   1 +
 gdb/aarch64-linux-tdep.c                      |   3 +-
 gdb/aarch64-tdep.c                            |  18 +-
 gdb/aarch64-tdep.h                            |   5 +-
 gdb/alpha-linux-tdep.c                        |   2 +-
 gdb/amd64-linux-tdep.c                        |  11 +-
 gdb/amd64-tdep.c                              |  23 +-
 gdb/amd64-tdep.h                              |   9 +-
 gdb/arc-linux-tdep.c                          |   2 +-
 gdb/arm-linux-tdep.c                          |  19 +-
 gdb/arm-tdep.c                                | 183 +++----
 gdb/arm-tdep.h                                |  18 +-
 gdb/bfin-linux-tdep.c                         |   2 +-
 gdb/cris-linux-tdep.c                         |   2 +-
 gdb/csky-linux-tdep.c                         |   2 +-
 gdb/displaced-stepping.c                      | 289 +++++++++++
 gdb/displaced-stepping.h                      | 200 ++++++++
 gdb/frv-linux-tdep.c                          |   2 +-
 gdb/gdbarch.c                                 | 117 ++++-
 gdb/gdbarch.h                                 |  47 +-
 gdb/gdbarch.sh                                |  26 +-
 gdb/gdbthread.h                               |  35 +-
 gdb/hppa-linux-tdep.c                         |   2 +-
 gdb/i386-linux-tdep.c                         |  31 +-
 gdb/i386-tdep.c                               |  14 +-
 gdb/i386-tdep.h                               |  12 +-
 gdb/ia64-linux-tdep.c                         |   2 +-
 gdb/inferior.h                                |   1 +
 gdb/infrun.c                                  | 449 ++++++++----------
 gdb/infrun.h                                  |  84 +---
 gdb/jit.c                                     |   9 +-
 gdb/jit.h                                     |   7 -
 gdb/linux-tdep.c                              | 114 ++++-
 gdb/linux-tdep.h                              |  27 +-
 gdb/m32r-linux-tdep.c                         |   2 +-
 gdb/m68k-linux-tdep.c                         |   2 +-
 gdb/microblaze-linux-tdep.c                   |   2 +-
 gdb/mips-linux-tdep.c                         |   2 +-
 gdb/mn10300-linux-tdep.c                      |   2 +-
 gdb/nios2-linux-tdep.c                        |   2 +-
 gdb/observable.c                              |   1 +
 gdb/observable.h                              |   3 +
 gdb/or1k-linux-tdep.c                         |   2 +-
 gdb/ppc-linux-tdep.c                          |   5 +-
 gdb/riscv-linux-tdep.c                        |   2 +-
 gdb/rs6000-aix-tdep.c                         |   6 +-
 gdb/rs6000-tdep.c                             |  94 +++-
 gdb/s390-linux-tdep.c                         |   2 +-
 gdb/s390-tdep.c                               |  22 +-
 gdb/sh-linux-tdep.c                           |   2 +-
 gdb/solib.c                                   |   4 +
 gdb/sparc-linux-tdep.c                        |   2 +-
 gdb/sparc64-linux-tdep.c                      |   2 +-
 .../gdb.arch/amd64-disp-step-avx.exp          |   2 +-
 .../forking-threads-plus-breakpoint.exp       |   2 +-
 .../gdb.threads/non-stop-fair-events.exp      |   2 +-
 gdb/thread.c                                  |  60 ++-
 gdb/tic6x-linux-tdep.c                        |   2 +-
 gdb/tilegx-linux-tdep.c                       |   2 +-
 gdb/xtensa-linux-tdep.c                       |   2 +-
 60 files changed, 1352 insertions(+), 647 deletions(-)
 create mode 100644 gdb/displaced-stepping.c
 create mode 100644 gdb/displaced-stepping.h

-- 
2.28.0


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

* [PATCH 01/12] gdb: add inferior_execd observable
  2020-11-10 21:46 [PATCH 00/12] Concurrent displaced stepping Simon Marchi
@ 2020-11-10 21:46 ` Simon Marchi
  2020-11-25  1:28   ` Pedro Alves
  2020-11-10 21:46 ` [PATCH 02/12] gdb: clear inferior displaced stepping state on exec Simon Marchi
                   ` (10 subsequent siblings)
  11 siblings, 1 reply; 44+ messages in thread
From: Simon Marchi @ 2020-11-10 21:46 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

I want to add another action (clearing displaced stepping state) that
happens when an inferior execs.  I think it would be cleaner to have an
observer for this event, rather than have infrun know about each other
sub-component.

Replace the calls to solib_create_inferior_hook and
jit_inferior_created_hook in follow_exec by observers.

gdb/ChangeLog:

	* observable.h (inferior_execd): Declare new observable.
	* observable.c (inferior_execd): Declare new observable.
	* infrun.c (follow_exec): Notify inferior_execd observer.
	* jit.c (jit_inferior_created_hook): Make static.
	(_initialize_jit): Register inferior_execd observer.
	* jit.h (jit_inferior_created_hook): Remove declaration.
	* solib.c (_initialize_solib): Register inferior_execd observer.

Change-Id: I000cce00094e23baa67df693d912646b6ae38e44
---
 gdb/infrun.c     | 4 +---
 gdb/jit.c        | 9 +++++++--
 gdb/jit.h        | 7 -------
 gdb/observable.c | 1 +
 gdb/observable.h | 3 +++
 gdb/solib.c      | 4 ++++
 6 files changed, 16 insertions(+), 12 deletions(-)

diff --git a/gdb/infrun.c b/gdb/infrun.c
index 990f40aa626..d59f6945285 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -1222,9 +1222,7 @@ follow_exec (ptid_t ptid, const char *exec_file_target)
      registers.  */
   target_find_description ();
 
-  solib_create_inferior_hook (0);
-
-  jit_inferior_created_hook (inf);
+  gdb::observers::inferior_execd.notify (inf);
 
   breakpoint_re_set ();
 
diff --git a/gdb/jit.c b/gdb/jit.c
index fd24d539159..9deeed7ab59 100644
--- a/gdb/jit.c
+++ b/gdb/jit.c
@@ -49,6 +49,7 @@ static const char jit_break_name[] = "__jit_debug_register_code";
 
 static const char jit_descriptor_name[] = "__jit_debug_descriptor";
 
+static void jit_inferior_created_hook (inferior *inf);
 static void jit_inferior_exit_hook (struct inferior *inf);
 
 /* An unwinder is registered for every gdbarch.  This key is used to
@@ -1230,9 +1231,12 @@ jit_inferior_init (inferior *inf)
     }
 }
 
-/* See jit.h.  */
+/* Looks for the descriptor and registration symbols and breakpoints
+   the registration function.  If it finds both, it registers all the
+   already JITed code.  If it has already found the symbols, then it
+   doesn't try again.  */
 
-void
+static void
 jit_inferior_created_hook (inferior *inf)
 {
   jit_inferior_init (inf);
@@ -1337,6 +1341,7 @@ _initialize_jit ()
 			     &setdebuglist, &showdebuglist);
 
   gdb::observers::inferior_created.attach (jit_inferior_created_hook);
+  gdb::observers::inferior_execd.attach (jit_inferior_created_hook);
   gdb::observers::inferior_exit.attach (jit_inferior_exit_hook);
   gdb::observers::breakpoint_deleted.attach (jit_breakpoint_deleted);
 
diff --git a/gdb/jit.h b/gdb/jit.h
index 6f972a6e077..96938060733 100644
--- a/gdb/jit.h
+++ b/gdb/jit.h
@@ -103,13 +103,6 @@ struct jited_objfile_data
   CORE_ADDR addr;
 };
 
-/* Looks for the descriptor and registration symbols and breakpoints
-   the registration function.  If it finds both, it registers all the
-   already JITed code.  If it has already found the symbols, then it
-   doesn't try again.  */
-
-extern void jit_inferior_created_hook (inferior *inf);
-
 /* Re-establish the jit breakpoint(s).  */
 
 extern void jit_breakpoint_re_set (void);
diff --git a/gdb/observable.c b/gdb/observable.c
index 81aa392cc21..231f955fa26 100644
--- a/gdb/observable.c
+++ b/gdb/observable.c
@@ -43,6 +43,7 @@ DEFINE_OBSERVABLE (command_error);
 DEFINE_OBSERVABLE (target_changed);
 DEFINE_OBSERVABLE (executable_changed);
 DEFINE_OBSERVABLE (inferior_created);
+DEFINE_OBSERVABLE (inferior_execd);
 DEFINE_OBSERVABLE (record_changed);
 DEFINE_OBSERVABLE (solib_loaded);
 DEFINE_OBSERVABLE (solib_unloaded);
diff --git a/gdb/observable.h b/gdb/observable.h
index 9114b28d04f..1dce6746ff3 100644
--- a/gdb/observable.h
+++ b/gdb/observable.h
@@ -89,6 +89,9 @@ extern observable<> executable_changed;
    information on the inferior has been printed.  */
 extern observable<inferior */* inferior */> inferior_created;
 
+/* The inferior INF has exec'ed a new executable file.  */
+extern observable<struct inferior */* inf */> inferior_execd;
+
 /* The status of process record for inferior inferior in gdb has
    changed.  The process record is started if STARTED is true, and
    the process record is stopped if STARTED is false.
diff --git a/gdb/solib.c b/gdb/solib.c
index b11c6f38900..1f6e91599a5 100644
--- a/gdb/solib.c
+++ b/gdb/solib.c
@@ -1557,6 +1557,10 @@ _initialize_solib ()
   solib_data = gdbarch_data_register_pre_init (solib_init);
 
   gdb::observers::free_objfile.attach (remove_user_added_objfile);
+  gdb::observers::inferior_execd.attach ([] (inferior *inf)
+    {
+      solib_create_inferior_hook (0);
+    });
 
   add_com ("sharedlibrary", class_files, sharedlibrary_command,
 	   _("Load shared object library symbols for files matching REGEXP."));
-- 
2.28.0


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

* [PATCH 02/12] gdb: clear inferior displaced stepping state on exec
  2020-11-10 21:46 [PATCH 00/12] Concurrent displaced stepping Simon Marchi
  2020-11-10 21:46 ` [PATCH 01/12] gdb: add inferior_execd observable Simon Marchi
@ 2020-11-10 21:46 ` Simon Marchi
  2020-11-25  1:28   ` Pedro Alves
  2020-11-10 21:46 ` [PATCH 03/12] gdb: rename things related to step over chains Simon Marchi
                   ` (9 subsequent siblings)
  11 siblings, 1 reply; 44+ messages in thread
From: Simon Marchi @ 2020-11-10 21:46 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

When a process does an exec, all its program space is replaced with the
newly loaded executable.  All non-main threads disappear and the main
thread starts executing at the entry point of the new executable.

Things can go wrong if a displaced step operation is in progress while
we process the exec event.

If the main thread is the one executing the displaced step: when that
thread (now executing in the new executable) stops somewhere (say, at a
breakpoint), displaced_step_fixup will run and clear up the state.  We
will execute the "fixup" phase for the instruction we single-stepped in
the old program space.  We are now in a completely different context,
so doing the fixup may corrupt the state.

If it is a non-main thread that is doing the displaced step: while
handling the exec event, GDB deletes the thread_info representing that
thread (since the thread doesn't exist in the inferior after the exec).
But inferior::displaced_step_state::step_thread will still point to it.
When handling events later, this condition, in displaced_step_fixup,
will likely never be true:

    /* Was this event for the thread we displaced?  */
    if (displaced->step_thread != event_thread)
      return 0;

... since displaced->step_thread points to a deleted thread (unless that
storage gets re-used for a new thread_info, but that wouldn't be good
either).  This effectively makes the displaced stepping buffer occupied
for ever.  When a thread in the new program space will want to do a
displaced step, it will wait for ever.

I think we simply need to reset the displaced stepping state of the
inferior on exec.  Everything execution-related that existed before the
exec is now gone.

I tried to write a test where a non-main thread displaced-steps an exec
syscall, where things would hang due to the displaced step buffer not
getting released.  However, due to PR 26754 [1], it is hard to make it
stable.  So I'm not including a test for this patch.  If you have an
idea for another way to test this without triggering this bug, I'd like
to hear it.

[1] https://sourceware.org/bugzilla/show_bug.cgi?id=26754

gdb/ChangeLog:

	* infrun.c (infrun_inferior_execd): New function.
	(_initialize_infrun): Attach inferior_execd observer.

Change-Id: I1bbc8538e683f53af5b980091849086f4fec5ff9
---
 gdb/infrun.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/gdb/infrun.c b/gdb/infrun.c
index d59f6945285..bb881f3510d 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -1528,6 +1528,12 @@ infrun_inferior_exit (struct inferior *inf)
   inf->displaced_step_state.reset ();
 }
 
+static void
+infrun_inferior_execd (inferior *inf)
+{
+  inf->displaced_step_state.reset ();
+}
+
 /* If ON, and the architecture supports it, GDB will use displaced
    stepping to step over breakpoints.  If OFF, or if the architecture
    doesn't support it, GDB will instead use the traditional
@@ -9509,6 +9515,7 @@ enabled by default on some platforms."),
   gdb::observers::thread_stop_requested.attach (infrun_thread_stop_requested);
   gdb::observers::thread_exit.attach (infrun_thread_thread_exit);
   gdb::observers::inferior_exit.attach (infrun_inferior_exit);
+  gdb::observers::inferior_execd.attach (infrun_inferior_execd);
 
   /* Explicitly create without lookup, since that tries to create a
      value with a void typed value, and when we get here, gdbarch
-- 
2.28.0


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

* [PATCH 03/12] gdb: rename things related to step over chains
  2020-11-10 21:46 [PATCH 00/12] Concurrent displaced stepping Simon Marchi
  2020-11-10 21:46 ` [PATCH 01/12] gdb: add inferior_execd observable Simon Marchi
  2020-11-10 21:46 ` [PATCH 02/12] gdb: clear inferior displaced stepping state on exec Simon Marchi
@ 2020-11-10 21:46 ` Simon Marchi
  2020-11-25  1:28   ` Pedro Alves
  2020-11-10 21:46 ` [PATCH 04/12] gdb: rename displaced_step_closure to displaced_step_copy_insn_closure Simon Marchi
                   ` (8 subsequent siblings)
  11 siblings, 1 reply; 44+ messages in thread
From: Simon Marchi @ 2020-11-10 21:46 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

Rename step_over_queue_head to global_thread_step_over_chain_head, to
make it more obvious when reading code that we are touching the global
queue.  Rename all functions that operate on it to have "global" in
their name, to make it clear on which chain they operate on.  Also, in a
subsequent patch, we'll need both global and non-global versions of
these functions, so it will be easier to do the distinction if they are
named properly.

Normalize the naming to use "chain" everywhere instead of sometimes
"queue", sometimes "chain".

I also reworded a few comments in gdbthread.h.  They implied that the
step over chain is per-inferior, when in reality there is only one
global chain, not one per inferior, as far as I understand.

gdb/ChangeLog:

        * gdbthread.h (thread_step_over_chain_enqueue): Rename to...
        (global_thread_step_over_chain_enqueue): ... this.  Update all
	users.
        (thread_step_over_chain_remove): Rename to...
        (global_thread_step_over_chain_remove): ... this.  Update all
	users.
        (thread_step_over_chain_next): Rename to...
        (global_thread_step_over_chain_next): ... this.  Update all
	users.
        * infrun.h (step_over_queue_head): Rename to...
	(global_thread_step_over_chain_head): ... this.  Update all
	users.
        * infrun.c (step_over_queue_head): Rename to...
	(global_thread_step_over_chain_head): ... this.  Update all
	users.
        * thread.c (step_over_chain_remove): Rename to...
	(thread_step_over_chain_remove): ... this.  Update all users.
	(thread_step_over_chain_next): Rename to...
        (global_thread_step_over_chain_next): ... this.  Update all
	users.
        (thread_step_over_chain_enqueue): Rename to...
        (global_thread_step_over_chain_enqueue): ... this.  Update all
	users.
        (thread_step_over_chain_remove): Rename to...
        (global_thread_step_over_chain_remove): ... this.  Update all
	users.

Change-Id: Iabbf57d83c01321ca199d83fadb57f5b04e4d6d9
---
 gdb/gdbthread.h | 16 ++++++++--------
 gdb/infrun.c    | 26 +++++++++++++-------------
 gdb/infrun.h    |  4 ++--
 gdb/thread.c    | 18 +++++++++---------
 4 files changed, 32 insertions(+), 32 deletions(-)

diff --git a/gdb/gdbthread.h b/gdb/gdbthread.h
index 630727e2fb5..b5a32087289 100644
--- a/gdb/gdbthread.h
+++ b/gdb/gdbthread.h
@@ -741,20 +741,20 @@ extern value *get_last_thread_stack_temporary (struct thread_info *tp);
 extern bool value_in_thread_stack_temporaries (struct value *,
 					       struct thread_info *thr);
 
-/* Add TP to the end of its inferior's pending step-over chain.  */
+/* Add TP to the end of the global pending step-over chain.  */
 
-extern void thread_step_over_chain_enqueue (struct thread_info *tp);
+extern void global_thread_step_over_chain_enqueue (struct thread_info *tp);
 
-/* Remove TP from its inferior's pending step-over chain.  */
+/* Remove TP from the global pending step-over chain.  */
 
-extern void thread_step_over_chain_remove (struct thread_info *tp);
+extern void global_thread_step_over_chain_remove (struct thread_info *tp);
 
-/* Return the next thread in the step-over chain starting at TP.  NULL
-   if TP is the last entry in the chain.  */
+/* Return the thread following TP in the global step-over chain, or NULL if TP
+   is the last entry in the chain.  */
 
-extern struct thread_info *thread_step_over_chain_next (struct thread_info *tp);
+extern struct thread_info *global_thread_step_over_chain_next (struct thread_info *tp);
 
-/* Return true if TP is in the step-over chain.  */
+/* Return true if TP is in any step-over chain.  */
 
 extern int thread_is_in_step_over_chain (struct thread_info *tp);
 
diff --git a/gdb/infrun.c b/gdb/infrun.c
index bb881f3510d..917a35c23ab 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -1237,14 +1237,14 @@ follow_exec (ptid_t ptid, const char *exec_file_target)
      matically get reset there in the new process.).  */
 }
 
-/* The queue of threads that need to do a step-over operation to get
+/* The chain of threads that need to do a step-over operation to get
    past e.g., a breakpoint.  What technique is used to step over the
    breakpoint/watchpoint does not matter -- all threads end up in the
    same queue, to maintain rough temporal order of execution, in order
    to avoid starvation, otherwise, we could e.g., find ourselves
    constantly stepping the same couple threads past their breakpoints
    over and over, if the single-step finish fast enough.  */
-struct thread_info *step_over_queue_head;
+struct thread_info *global_thread_step_over_chain_head;
 
 /* Bit flags indicating what the thread needs to step over.  */
 
@@ -1692,7 +1692,7 @@ displaced_step_prepare_throw (thread_info *tp)
       displaced_debug_printf ("deferring step of %s",
 			      target_pid_to_str (tp->ptid).c_str ());
 
-      thread_step_over_chain_enqueue (tp);
+      global_thread_step_over_chain_enqueue (tp);
       return 0;
     }
   else
@@ -1947,7 +1947,7 @@ start_step_over (void)
   if (step_over_info_valid_p ())
     return false;
 
-  for (tp = step_over_queue_head; tp != NULL; tp = next)
+  for (tp = global_thread_step_over_chain_head; tp != NULL; tp = next)
     {
       struct execution_control_state ecss;
       struct execution_control_state *ecs = &ecss;
@@ -1956,7 +1956,7 @@ start_step_over (void)
 
       gdb_assert (!tp->stop_requested);
 
-      next = thread_step_over_chain_next (tp);
+      next = global_thread_step_over_chain_next (tp);
 
       /* If this inferior already has a displaced step in process,
 	 don't start a new one.  */
@@ -1974,9 +1974,9 @@ start_step_over (void)
       if (must_be_in_line && displaced_step_in_progress_any_inferior ())
 	return false;
 
-      thread_step_over_chain_remove (tp);
+      global_thread_step_over_chain_remove (tp);
 
-      if (step_over_queue_head == NULL)
+      if (global_thread_step_over_chain_head == NULL)
 	infrun_debug_printf ("step-over queue now empty");
 
       if (tp->control.trap_expected
@@ -3033,7 +3033,7 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal)
 	  infrun_debug_printf ("need to step-over [%s] first",
 			       target_pid_to_str (tp->ptid).c_str ());
 
-	  thread_step_over_chain_enqueue (tp);
+	  global_thread_step_over_chain_enqueue (tp);
 	}
 
       switch_to_thread (cur_thr);
@@ -3042,7 +3042,7 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal)
   /* Enqueue the current thread last, so that we move all other
      threads over their breakpoints first.  */
   if (cur_thr->stepping_over_breakpoint)
-    thread_step_over_chain_enqueue (cur_thr);
+    global_thread_step_over_chain_enqueue (cur_thr);
 
   /* If the thread isn't started, we'll still need to set its prev_pc,
      so that switch_back_to_stepped_thread knows the thread hasn't
@@ -3226,7 +3226,7 @@ infrun_thread_stop_requested (ptid_t ptid)
 	 start_step_over doesn't try to resume them
 	 automatically.  */
       if (thread_is_in_step_over_chain (tp))
-	thread_step_over_chain_remove (tp);
+	global_thread_step_over_chain_remove (tp);
 
       /* If the thread is stopped, but the user/frontend doesn't
 	 know about that yet, queue a pending event, as if the
@@ -4830,7 +4830,7 @@ stop_all_threads (void)
 			      target_pid_to_str (t->ptid).c_str ());
 
 			  t->control.trap_expected = 0;
-			  thread_step_over_chain_enqueue (t);
+			  global_thread_step_over_chain_enqueue (t);
 			}
 		    }
 		  else
@@ -4853,7 +4853,7 @@ stop_all_threads (void)
 			{
 			  /* Add it back to the step-over queue.  */
 			  t->control.trap_expected = 0;
-			  thread_step_over_chain_enqueue (t);
+			  global_thread_step_over_chain_enqueue (t);
 			}
 
 		      regcache = get_thread_regcache (t);
@@ -7781,7 +7781,7 @@ keep_going_pass_signal (struct execution_control_state *ecs)
 	  infrun_debug_printf ("step-over already in progress: "
 			       "step-over for %s deferred",
 			       target_pid_to_str (tp->ptid).c_str ());
-	  thread_step_over_chain_enqueue (tp);
+	  global_thread_step_over_chain_enqueue (tp);
 	}
       else
 	{
diff --git a/gdb/infrun.h b/gdb/infrun.h
index 2084d3d16b5..ca0774e8e6b 100644
--- a/gdb/infrun.h
+++ b/gdb/infrun.h
@@ -262,9 +262,9 @@ extern void infrun_async (int enable);
    loop.  */
 extern void mark_infrun_async_event_handler (void);
 
-/* The global queue of threads that need to do a step-over operation
+/* The global chain of threads that need to do a step-over operation
    to get past e.g., a breakpoint.  */
-extern struct thread_info *step_over_queue_head;
+extern struct thread_info *global_thread_step_over_chain_head;
 
 /* Remove breakpoints if possible (usually that means, if everything
    is stopped).  On failure, print a message.  */
diff --git a/gdb/thread.c b/gdb/thread.c
index 32d14e8662c..ceb19df1db6 100644
--- a/gdb/thread.c
+++ b/gdb/thread.c
@@ -207,7 +207,7 @@ set_thread_exited (thread_info *tp, bool silent)
 {
   /* Dead threads don't need to step-over.  Remove from queue.  */
   if (tp->step_over_next != NULL)
-    thread_step_over_chain_remove (tp);
+    global_thread_step_over_chain_remove (tp);
 
   if (tp->state != THREAD_EXITED)
     {
@@ -365,7 +365,7 @@ step_over_chain_enqueue (struct thread_info **list_p, struct thread_info *tp)
 /* Remove TP from step-over chain LIST_P.  */
 
 static void
-step_over_chain_remove (struct thread_info **list_p, struct thread_info *tp)
+thread_step_over_chain_remove (struct thread_info **list_p, struct thread_info *tp)
 {
   gdb_assert (tp->step_over_next != NULL);
   gdb_assert (tp->step_over_prev != NULL);
@@ -386,11 +386,11 @@ step_over_chain_remove (struct thread_info **list_p, struct thread_info *tp)
 /* See gdbthread.h.  */
 
 struct thread_info *
-thread_step_over_chain_next (struct thread_info *tp)
+global_thread_step_over_chain_next (struct thread_info *tp)
 {
   struct thread_info *next = tp->step_over_next;
 
-  return (next == step_over_queue_head ? NULL : next);
+  return (next == global_thread_step_over_chain_head ? NULL : next);
 }
 
 /* See gdbthread.h.  */
@@ -404,17 +404,17 @@ thread_is_in_step_over_chain (struct thread_info *tp)
 /* See gdbthread.h.  */
 
 void
-thread_step_over_chain_enqueue (struct thread_info *tp)
+global_thread_step_over_chain_enqueue (struct thread_info *tp)
 {
-  step_over_chain_enqueue (&step_over_queue_head, tp);
+  step_over_chain_enqueue (&global_thread_step_over_chain_head, tp);
 }
 
 /* See gdbthread.h.  */
 
 void
-thread_step_over_chain_remove (struct thread_info *tp)
+global_thread_step_over_chain_remove (struct thread_info *tp)
 {
-  step_over_chain_remove (&step_over_queue_head, tp);
+  thread_step_over_chain_remove (&global_thread_step_over_chain_head, tp);
 }
 
 /* Delete the thread referenced by THR.  If SILENT, don't notify
@@ -805,7 +805,7 @@ set_running_thread (struct thread_info *tp, bool running)
 	 the step-over queue, so that we don't try to resume
 	 it until the user wants it to.  */
       if (tp->step_over_next != NULL)
-	thread_step_over_chain_remove (tp);
+	global_thread_step_over_chain_remove (tp);
     }
 
   return started;
-- 
2.28.0


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

* [PATCH 04/12] gdb: rename displaced_step_closure to displaced_step_copy_insn_closure
  2020-11-10 21:46 [PATCH 00/12] Concurrent displaced stepping Simon Marchi
                   ` (2 preceding siblings ...)
  2020-11-10 21:46 ` [PATCH 03/12] gdb: rename things related to step over chains Simon Marchi
@ 2020-11-10 21:46 ` Simon Marchi
  2020-11-25  1:29   ` Pedro Alves
  2020-11-10 21:46 ` [PATCH 05/12] gdb: rename displaced_step_fixup to displaced_step_finish Simon Marchi
                   ` (7 subsequent siblings)
  11 siblings, 1 reply; 44+ messages in thread
From: Simon Marchi @ 2020-11-10 21:46 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

Since we're going to introduce other "displaced step" functions and
another kind of displaced step closure, make it clear that this is the
return type of the gdbarch_displaced_step_copy_insn function.

gdb/ChangeLog:

	* infrun.h (get_displaced_step_closure_by_addr): Rename to...
	(get_displaced_step_copy_insn_closure_by_addr): ... this.
	Update all users.
	(displaced_step_closure): Rename to...
	(displaced_step_copy_insn_closure): ... this.  Update all users.
	(displaced_step_closure_up): Rename to...
	(displaced_step_copy_insn_closure_up). ... this.  Update all
	users.
	(buf_displaced_step_closure): Rename to...
	(buf_displaced_step_copy_insn_closure): ... this.  Update all
	users.
	* infrun.c (get_displaced_step_closure_by_addr): Rename to...
	(get_displaced_step_copy_insn_closure_by_addr): ... this.
	Update all users.
	* aarch64-tdep.c (aarch64_displaced_step_closure): Rename to...
	(aarch64_displaced_step_copy_insn_closure): ... this.  Update
	all users.
	* amd64-tdep.c (amd64_displaced_step_closure): Rename to...
	(amd64_displaced_step_copy_insn_closure): ... this.  Update all
	users.
	* arm-tdep.h (arm_displaced_step_closure): Rename to...
	(arm_displaced_step_copy_insn_closure): ... this.  Update all
	users.
	* i386-tdep.h (i386_displaced_step_closure): Rename to...
	(i386_displaced_step_copy_insn_closure): ... this.  Update all
	users.
	* rs6000-tdep.c (ppc_displaced_step_closure): Rename to...
	(ppc_displaced_step_copy_insn_closure): ... this.  Update all
	users.
	* s390-tdep.c (s390_displaced_step_closure): Rename to...
	(s390_displaced_step_copy_insn_closure): ... this.  Update all
	users.
	* gdbarch.h: Re-generate.
	* gdbarch.c: Re-generate.

Change-Id: I11f56dbcd4c3532fb195a08ba93bccf1d12a03c8
---
 gdb/aarch64-tdep.c    |  18 +++--
 gdb/aarch64-tdep.h    |   4 +-
 gdb/amd64-tdep.c      |  23 +++---
 gdb/amd64-tdep.h      |   9 +--
 gdb/arm-linux-tdep.c  |  16 ++--
 gdb/arm-tdep.c        | 180 +++++++++++++++++++++---------------------
 gdb/arm-tdep.h        |  18 +++--
 gdb/gdbarch.c         |   4 +-
 gdb/gdbarch.h         |   8 +-
 gdb/gdbarch.sh        |   4 +-
 gdb/i386-linux-tdep.c |  27 ++++---
 gdb/i386-tdep.c       |  14 ++--
 gdb/i386-tdep.h       |  12 +--
 gdb/infrun.c          |  13 +--
 gdb/infrun.h          |  17 ++--
 gdb/rs6000-tdep.c     |  16 ++--
 gdb/s390-tdep.c       |  17 ++--
 17 files changed, 207 insertions(+), 193 deletions(-)

diff --git a/gdb/aarch64-tdep.c b/gdb/aarch64-tdep.c
index 2c1d888904a..a0b7d2e72bb 100644
--- a/gdb/aarch64-tdep.c
+++ b/gdb/aarch64-tdep.c
@@ -2903,7 +2903,8 @@ aarch64_software_single_step (struct regcache *regcache)
   return next_pcs;
 }
 
-struct aarch64_displaced_step_closure : public displaced_step_closure
+struct aarch64_displaced_step_copy_insn_closure
+  : public displaced_step_copy_insn_closure
 {
   /* It is true when condition instruction, such as B.CON, TBZ, etc,
      is being displaced stepping.  */
@@ -2929,7 +2930,7 @@ struct aarch64_displaced_step_data
   /* Registers when doing displaced stepping.  */
   struct regcache *regs;
 
-  aarch64_displaced_step_closure *dsc;
+  aarch64_displaced_step_copy_insn_closure *dsc;
 };
 
 /* Implementation of aarch64_insn_visitor method "b".  */
@@ -3138,7 +3139,7 @@ static const struct aarch64_insn_visitor visitor =
 
 /* Implement the "displaced_step_copy_insn" gdbarch method.  */
 
-displaced_step_closure_up
+displaced_step_copy_insn_closure_up
 aarch64_displaced_step_copy_insn (struct gdbarch *gdbarch,
 				  CORE_ADDR from, CORE_ADDR to,
 				  struct regcache *regs)
@@ -3158,8 +3159,8 @@ aarch64_displaced_step_copy_insn (struct gdbarch *gdbarch,
       return NULL;
     }
 
-  std::unique_ptr<aarch64_displaced_step_closure> dsc
-    (new aarch64_displaced_step_closure);
+  std::unique_ptr<aarch64_displaced_step_copy_insn_closure> dsc
+    (new aarch64_displaced_step_copy_insn_closure);
   dsd.base.insn_addr = from;
   dsd.new_addr = to;
   dsd.regs = regs;
@@ -3191,18 +3192,19 @@ aarch64_displaced_step_copy_insn (struct gdbarch *gdbarch,
     }
 
   /* This is a work around for a problem with g++ 4.8.  */
-  return displaced_step_closure_up (dsc.release ());
+  return displaced_step_copy_insn_closure_up (dsc.release ());
 }
 
 /* Implement the "displaced_step_fixup" gdbarch method.  */
 
 void
 aarch64_displaced_step_fixup (struct gdbarch *gdbarch,
-			      struct displaced_step_closure *dsc_,
+			      struct displaced_step_copy_insn_closure *dsc_,
 			      CORE_ADDR from, CORE_ADDR to,
 			      struct regcache *regs)
 {
-  aarch64_displaced_step_closure *dsc = (aarch64_displaced_step_closure *) dsc_;
+  aarch64_displaced_step_copy_insn_closure *dsc
+    = (aarch64_displaced_step_copy_insn_closure *) dsc_;
 
   ULONGEST pc;
 
diff --git a/gdb/aarch64-tdep.h b/gdb/aarch64-tdep.h
index 81ce4d84b41..895aa5977f0 100644
--- a/gdb/aarch64-tdep.h
+++ b/gdb/aarch64-tdep.h
@@ -107,13 +107,13 @@ const target_desc *aarch64_read_description (uint64_t vq, bool pauth_p);
 extern int aarch64_process_record (struct gdbarch *gdbarch,
 			       struct regcache *regcache, CORE_ADDR addr);
 
-displaced_step_closure_up
+displaced_step_copy_insn_closure_up
   aarch64_displaced_step_copy_insn (struct gdbarch *gdbarch,
 				    CORE_ADDR from, CORE_ADDR to,
 				    struct regcache *regs);
 
 void aarch64_displaced_step_fixup (struct gdbarch *gdbarch,
-				   struct displaced_step_closure *dsc,
+				   displaced_step_copy_insn_closure *dsc,
 				   CORE_ADDR from, CORE_ADDR to,
 				   struct regcache *regs);
 
diff --git a/gdb/amd64-tdep.c b/gdb/amd64-tdep.c
index 31d4d3460ef..d2bcbe199bc 100644
--- a/gdb/amd64-tdep.c
+++ b/gdb/amd64-tdep.c
@@ -1103,9 +1103,10 @@ struct amd64_insn
   gdb_byte *raw_insn;
 };
 
-struct amd64_displaced_step_closure : public displaced_step_closure
+struct amd64_displaced_step_copy_insn_closure
+  : public displaced_step_copy_insn_closure
 {
-  amd64_displaced_step_closure (int insn_buf_len)
+  amd64_displaced_step_copy_insn_closure (int insn_buf_len)
   : insn_buf (insn_buf_len, 0)
   {}
 
@@ -1386,7 +1387,8 @@ amd64_get_insn_details (gdb_byte *insn, struct amd64_insn *details)
    We set base = pc + insn_length so we can leave disp unchanged.  */
 
 static void
-fixup_riprel (struct gdbarch *gdbarch, amd64_displaced_step_closure *dsc,
+fixup_riprel (struct gdbarch *gdbarch,
+	      amd64_displaced_step_copy_insn_closure *dsc,
 	      CORE_ADDR from, CORE_ADDR to, struct regcache *regs)
 {
   const struct amd64_insn *insn_details = &dsc->insn_details;
@@ -1447,7 +1449,7 @@ fixup_riprel (struct gdbarch *gdbarch, amd64_displaced_step_closure *dsc,
 
 static void
 fixup_displaced_copy (struct gdbarch *gdbarch,
-		      amd64_displaced_step_closure *dsc,
+		      amd64_displaced_step_copy_insn_closure *dsc,
 		      CORE_ADDR from, CORE_ADDR to, struct regcache *regs)
 {
   const struct amd64_insn *details = &dsc->insn_details;
@@ -1465,7 +1467,7 @@ fixup_displaced_copy (struct gdbarch *gdbarch,
     }
 }
 
-displaced_step_closure_up
+displaced_step_copy_insn_closure_up
 amd64_displaced_step_copy_insn (struct gdbarch *gdbarch,
 				CORE_ADDR from, CORE_ADDR to,
 				struct regcache *regs)
@@ -1474,8 +1476,8 @@ amd64_displaced_step_copy_insn (struct gdbarch *gdbarch,
   /* Extra space for sentinels so fixup_{riprel,displaced_copy} don't have to
      continually watch for running off the end of the buffer.  */
   int fixup_sentinel_space = len;
-  std::unique_ptr<amd64_displaced_step_closure> dsc
-    (new amd64_displaced_step_closure (len + fixup_sentinel_space));
+  std::unique_ptr<amd64_displaced_step_copy_insn_closure> dsc
+    (new amd64_displaced_step_copy_insn_closure (len + fixup_sentinel_space));
   gdb_byte *buf = &dsc->insn_buf[0];
   struct amd64_insn *details = &dsc->insn_details;
 
@@ -1509,7 +1511,7 @@ amd64_displaced_step_copy_insn (struct gdbarch *gdbarch,
 			  displaced_step_dump_bytes (buf, len).c_str ());
 
   /* This is a work around for a problem with g++ 4.8.  */
-  return displaced_step_closure_up (dsc.release ());
+  return displaced_step_copy_insn_closure_up (dsc.release ());
 }
 
 static int
@@ -1671,11 +1673,12 @@ amd64_insn_is_jump (struct gdbarch *gdbarch, CORE_ADDR addr)
 
 void
 amd64_displaced_step_fixup (struct gdbarch *gdbarch,
-			    struct displaced_step_closure *dsc_,
+			    struct displaced_step_copy_insn_closure *dsc_,
 			    CORE_ADDR from, CORE_ADDR to,
 			    struct regcache *regs)
 {
-  amd64_displaced_step_closure *dsc = (amd64_displaced_step_closure *) dsc_;
+  amd64_displaced_step_copy_insn_closure *dsc
+    = (amd64_displaced_step_copy_insn_closure *) dsc_;
   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
   /* The offset we applied to the instruction's address.  */
   ULONGEST insn_offset = to - from;
diff --git a/gdb/amd64-tdep.h b/gdb/amd64-tdep.h
index a4e69565593..56aef6c5d9a 100644
--- a/gdb/amd64-tdep.h
+++ b/gdb/amd64-tdep.h
@@ -88,13 +88,12 @@ enum amd64_regnum
 
 #define AMD64_NUM_REGS		(AMD64_GSBASE_REGNUM + 1)
 
-extern displaced_step_closure_up amd64_displaced_step_copy_insn
+extern displaced_step_copy_insn_closure_up amd64_displaced_step_copy_insn
   (struct gdbarch *gdbarch, CORE_ADDR from, CORE_ADDR to,
    struct regcache *regs);
-extern void amd64_displaced_step_fixup (struct gdbarch *gdbarch,
-					struct displaced_step_closure *closure,
-					CORE_ADDR from, CORE_ADDR to,
-					struct regcache *regs);
+extern void amd64_displaced_step_fixup
+  (struct gdbarch *gdbarch, displaced_step_copy_insn_closure *closure,
+   CORE_ADDR from, CORE_ADDR to, struct regcache *regs);
 
 /* Initialize the ABI for amd64.  Uses DEFAULT_TDESC as fallback
    tdesc, if INFO does not specify one.  */
diff --git a/gdb/arm-linux-tdep.c b/gdb/arm-linux-tdep.c
index 2d563c2b024..9caae06adfe 100644
--- a/gdb/arm-linux-tdep.c
+++ b/gdb/arm-linux-tdep.c
@@ -946,7 +946,7 @@ arm_linux_software_single_step (struct regcache *regcache)
 static void
 arm_linux_cleanup_svc (struct gdbarch *gdbarch,
 		       struct regcache *regs,
-		       arm_displaced_step_closure *dsc)
+		       arm_displaced_step_copy_insn_closure *dsc)
 {
   ULONGEST apparent_pc;
   int within_scratch;
@@ -970,7 +970,7 @@ arm_linux_cleanup_svc (struct gdbarch *gdbarch,
 
 static int
 arm_linux_copy_svc (struct gdbarch *gdbarch, struct regcache *regs,
-		    arm_displaced_step_closure *dsc)
+		    arm_displaced_step_copy_insn_closure *dsc)
 {
   CORE_ADDR return_to = 0;
 
@@ -1056,7 +1056,7 @@ arm_linux_copy_svc (struct gdbarch *gdbarch, struct regcache *regs,
 static void
 cleanup_kernel_helper_return (struct gdbarch *gdbarch,
 			      struct regcache *regs,
-			      arm_displaced_step_closure *dsc)
+			      arm_displaced_step_copy_insn_closure *dsc)
 {
   displaced_write_reg (regs, dsc, ARM_LR_REGNUM, dsc->tmp[0], CANNOT_WRITE_PC);
   displaced_write_reg (regs, dsc, ARM_PC_REGNUM, dsc->tmp[0], BRANCH_WRITE_PC);
@@ -1065,7 +1065,7 @@ cleanup_kernel_helper_return (struct gdbarch *gdbarch,
 static void
 arm_catch_kernel_helper_return (struct gdbarch *gdbarch, CORE_ADDR from,
 				CORE_ADDR to, struct regcache *regs,
-				arm_displaced_step_closure *dsc)
+				arm_displaced_step_copy_insn_closure *dsc)
 {
   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
 
@@ -1094,13 +1094,13 @@ arm_catch_kernel_helper_return (struct gdbarch *gdbarch, CORE_ADDR from,
    the program has stepped into a Linux kernel helper routine (which must be
    handled as a special case).  */
 
-static displaced_step_closure_up
+static displaced_step_copy_insn_closure_up
 arm_linux_displaced_step_copy_insn (struct gdbarch *gdbarch,
 				    CORE_ADDR from, CORE_ADDR to,
 				    struct regcache *regs)
 {
-  std::unique_ptr<arm_displaced_step_closure> dsc
-    (new arm_displaced_step_closure);
+  std::unique_ptr<arm_displaced_step_copy_insn_closure> dsc
+    (new arm_displaced_step_copy_insn_closure);
 
   /* Detect when we enter an (inaccessible by GDB) Linux kernel helper, and
      stop at the return location.  */
@@ -1122,7 +1122,7 @@ arm_linux_displaced_step_copy_insn (struct gdbarch *gdbarch,
   arm_displaced_init_closure (gdbarch, from, to, dsc.get ());
 
   /* This is a work around for a problem with g++ 4.8.  */
-  return displaced_step_closure_up (dsc.release ());
+  return displaced_step_copy_insn_closure_up (dsc.release ());
 }
 
 /* Implementation of `gdbarch_stap_is_single_operand', as defined in
diff --git a/gdb/arm-tdep.c b/gdb/arm-tdep.c
index 82e8ec4df49..e216ecf6fe5 100644
--- a/gdb/arm-tdep.c
+++ b/gdb/arm-tdep.c
@@ -410,9 +410,9 @@ arm_pc_is_thumb (struct gdbarch *gdbarch, CORE_ADDR memaddr)
 {
   struct bound_minimal_symbol sym;
   char type;
-  arm_displaced_step_closure *dsc
-    = ((arm_displaced_step_closure * )
-	get_displaced_step_closure_by_addr (memaddr));
+  arm_displaced_step_copy_insn_closure *dsc
+    = ((arm_displaced_step_copy_insn_closure * )
+	get_displaced_step_copy_insn_closure_by_addr (memaddr));
 
   /* If checking the mode of displaced instruction in copy area, the mode
      should be determined by instruction on the original address.  */
@@ -4436,7 +4436,7 @@ arm_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
    location.  */
 
 ULONGEST
-displaced_read_reg (struct regcache *regs, arm_displaced_step_closure *dsc,
+displaced_read_reg (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
 		    int regno)
 {
   ULONGEST ret;
@@ -4484,7 +4484,7 @@ displaced_in_arm_mode (struct regcache *regs)
 /* Write to the PC as from a branch instruction.  */
 
 static void
-branch_write_pc (struct regcache *regs, arm_displaced_step_closure *dsc,
+branch_write_pc (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
 		 ULONGEST val)
 {
   if (!dsc->is_thumb)
@@ -4530,7 +4530,7 @@ bx_write_pc (struct regcache *regs, ULONGEST val)
 /* Write to the PC as if from a load instruction.  */
 
 static void
-load_write_pc (struct regcache *regs, arm_displaced_step_closure *dsc,
+load_write_pc (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
 	       ULONGEST val)
 {
   if (DISPLACED_STEPPING_ARCH_VERSION >= 5)
@@ -4542,7 +4542,7 @@ load_write_pc (struct regcache *regs, arm_displaced_step_closure *dsc,
 /* Write to the PC as if from an ALU instruction.  */
 
 static void
-alu_write_pc (struct regcache *regs, arm_displaced_step_closure *dsc,
+alu_write_pc (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
 	      ULONGEST val)
 {
   if (DISPLACED_STEPPING_ARCH_VERSION >= 7 && !dsc->is_thumb)
@@ -4556,7 +4556,7 @@ alu_write_pc (struct regcache *regs, arm_displaced_step_closure *dsc,
    this is controlled by the WRITE_PC argument.  */
 
 void
-displaced_write_reg (struct regcache *regs, arm_displaced_step_closure *dsc,
+displaced_write_reg (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
 		     int regno, ULONGEST val, enum pc_write_style write_pc)
 {
   if (regno == ARM_PC_REGNUM)
@@ -4637,8 +4637,8 @@ insn_references_pc (uint32_t insn, uint32_t bitmask)
    matter what address they are executed at: in those cases, use this.  */
 
 static int
-arm_copy_unmodified (struct gdbarch *gdbarch, uint32_t insn,
-		     const char *iname, arm_displaced_step_closure *dsc)
+arm_copy_unmodified (struct gdbarch *gdbarch, uint32_t insn, const char *iname,
+		     arm_displaced_step_copy_insn_closure *dsc)
 {
   displaced_debug_printf ("copying insn %.8lx, opcode/class '%s' unmodified",
 			  (unsigned long) insn, iname);
@@ -4651,7 +4651,7 @@ arm_copy_unmodified (struct gdbarch *gdbarch, uint32_t insn,
 static int
 thumb_copy_unmodified_32bit (struct gdbarch *gdbarch, uint16_t insn1,
 			     uint16_t insn2, const char *iname,
-			     arm_displaced_step_closure *dsc)
+			     arm_displaced_step_copy_insn_closure *dsc)
 {
   displaced_debug_printf ("copying insn %.4x %.4x, opcode/class '%s' "
 			  "unmodified", insn1, insn2, iname);
@@ -4668,7 +4668,7 @@ thumb_copy_unmodified_32bit (struct gdbarch *gdbarch, uint16_t insn1,
 static int
 thumb_copy_unmodified_16bit (struct gdbarch *gdbarch, uint16_t insn,
 			     const char *iname,
-			     arm_displaced_step_closure *dsc)
+			     arm_displaced_step_copy_insn_closure *dsc)
 {
   displaced_debug_printf ("copying insn %.4x, opcode/class '%s' unmodified",
 			  insn, iname);
@@ -4681,8 +4681,8 @@ thumb_copy_unmodified_16bit (struct gdbarch *gdbarch, uint16_t insn,
 /* Preload instructions with immediate offset.  */
 
 static void
-cleanup_preload (struct gdbarch *gdbarch,
-		 struct regcache *regs, arm_displaced_step_closure *dsc)
+cleanup_preload (struct gdbarch *gdbarch, regcache *regs,
+		 arm_displaced_step_copy_insn_closure *dsc)
 {
   displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
   if (!dsc->u.preload.immed)
@@ -4691,7 +4691,7 @@ cleanup_preload (struct gdbarch *gdbarch,
 
 static void
 install_preload (struct gdbarch *gdbarch, struct regcache *regs,
-		 arm_displaced_step_closure *dsc, unsigned int rn)
+		 arm_displaced_step_copy_insn_closure *dsc, unsigned int rn)
 {
   ULONGEST rn_val;
   /* Preload instructions:
@@ -4710,7 +4710,7 @@ install_preload (struct gdbarch *gdbarch, struct regcache *regs,
 
 static int
 arm_copy_preload (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
-		  arm_displaced_step_closure *dsc)
+		  arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int rn = bits (insn, 16, 19);
 
@@ -4728,7 +4728,7 @@ arm_copy_preload (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
 
 static int
 thumb2_copy_preload (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
-		     struct regcache *regs, arm_displaced_step_closure *dsc)
+		     regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int rn = bits (insn1, 0, 3);
   unsigned int u_bit = bit (insn1, 7);
@@ -4776,7 +4776,7 @@ thumb2_copy_preload (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
 
 static void
 install_preload_reg(struct gdbarch *gdbarch, struct regcache *regs,
-		    arm_displaced_step_closure *dsc, unsigned int rn,
+		    arm_displaced_step_copy_insn_closure *dsc, unsigned int rn,
 		    unsigned int rm)
 {
   ULONGEST rn_val, rm_val;
@@ -4801,7 +4801,7 @@ install_preload_reg(struct gdbarch *gdbarch, struct regcache *regs,
 static int
 arm_copy_preload_reg (struct gdbarch *gdbarch, uint32_t insn,
 		      struct regcache *regs,
-		      arm_displaced_step_closure *dsc)
+		      arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int rn = bits (insn, 16, 19);
   unsigned int rm = bits (insn, 0, 3);
@@ -4824,7 +4824,7 @@ arm_copy_preload_reg (struct gdbarch *gdbarch, uint32_t insn,
 static void
 cleanup_copro_load_store (struct gdbarch *gdbarch,
 			  struct regcache *regs,
-			  arm_displaced_step_closure *dsc)
+			  arm_displaced_step_copy_insn_closure *dsc)
 {
   ULONGEST rn_val = displaced_read_reg (regs, dsc, 0);
 
@@ -4836,7 +4836,7 @@ cleanup_copro_load_store (struct gdbarch *gdbarch,
 
 static void
 install_copro_load_store (struct gdbarch *gdbarch, struct regcache *regs,
-			  arm_displaced_step_closure *dsc,
+			  arm_displaced_step_copy_insn_closure *dsc,
 			  int writeback, unsigned int rn)
 {
   ULONGEST rn_val;
@@ -4864,7 +4864,7 @@ install_copro_load_store (struct gdbarch *gdbarch, struct regcache *regs,
 static int
 arm_copy_copro_load_store (struct gdbarch *gdbarch, uint32_t insn,
 			   struct regcache *regs,
-			   arm_displaced_step_closure *dsc)
+			   arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int rn = bits (insn, 16, 19);
 
@@ -4884,7 +4884,7 @@ arm_copy_copro_load_store (struct gdbarch *gdbarch, uint32_t insn,
 static int
 thumb2_copy_copro_load_store (struct gdbarch *gdbarch, uint16_t insn1,
 			      uint16_t insn2, struct regcache *regs,
-			      arm_displaced_step_closure *dsc)
+			      arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int rn = bits (insn1, 0, 3);
 
@@ -4911,7 +4911,7 @@ thumb2_copy_copro_load_store (struct gdbarch *gdbarch, uint16_t insn1,
 
 static void
 cleanup_branch (struct gdbarch *gdbarch, struct regcache *regs,
-		arm_displaced_step_closure *dsc)
+		arm_displaced_step_copy_insn_closure *dsc)
 {
   uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
   int branch_taken = condition_true (dsc->u.branch.cond, status);
@@ -4942,7 +4942,7 @@ cleanup_branch (struct gdbarch *gdbarch, struct regcache *regs,
 
 static void
 install_b_bl_blx (struct gdbarch *gdbarch, struct regcache *regs,
-		  arm_displaced_step_closure *dsc,
+		  arm_displaced_step_copy_insn_closure *dsc,
 		  unsigned int cond, int exchange, int link, long offset)
 {
   /* Implement "BL<cond> <label>" as:
@@ -4971,7 +4971,7 @@ install_b_bl_blx (struct gdbarch *gdbarch, struct regcache *regs,
 }
 static int
 arm_copy_b_bl_blx (struct gdbarch *gdbarch, uint32_t insn,
-		   struct regcache *regs, arm_displaced_step_closure *dsc)
+		   regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int cond = bits (insn, 28, 31);
   int exchange = (cond == 0xf);
@@ -5000,7 +5000,7 @@ arm_copy_b_bl_blx (struct gdbarch *gdbarch, uint32_t insn,
 static int
 thumb2_copy_b_bl_blx (struct gdbarch *gdbarch, uint16_t insn1,
 		      uint16_t insn2, struct regcache *regs,
-		      arm_displaced_step_closure *dsc)
+		      arm_displaced_step_copy_insn_closure *dsc)
 {
   int link = bit (insn2, 14);
   int exchange = link && !bit (insn2, 12);
@@ -5053,7 +5053,7 @@ thumb2_copy_b_bl_blx (struct gdbarch *gdbarch, uint16_t insn1,
 /* Copy B Thumb instructions.  */
 static int
 thumb_copy_b (struct gdbarch *gdbarch, uint16_t insn,
-	      arm_displaced_step_closure *dsc)
+	      arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int cond = 0;
   int offset = 0;
@@ -5091,7 +5091,7 @@ thumb_copy_b (struct gdbarch *gdbarch, uint16_t insn,
 
 static void
 install_bx_blx_reg (struct gdbarch *gdbarch, struct regcache *regs,
-		    arm_displaced_step_closure *dsc, int link,
+		    arm_displaced_step_copy_insn_closure *dsc, int link,
 		    unsigned int cond, unsigned int rm)
 {
   /* Implement {BX,BLX}<cond> <reg>" as:
@@ -5114,7 +5114,7 @@ install_bx_blx_reg (struct gdbarch *gdbarch, struct regcache *regs,
 
 static int
 arm_copy_bx_blx_reg (struct gdbarch *gdbarch, uint32_t insn,
-		     struct regcache *regs, arm_displaced_step_closure *dsc)
+		     regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int cond = bits (insn, 28, 31);
   /* BX:  x12xxx1x
@@ -5133,7 +5133,7 @@ arm_copy_bx_blx_reg (struct gdbarch *gdbarch, uint32_t insn,
 static int
 thumb_copy_bx_blx_reg (struct gdbarch *gdbarch, uint16_t insn,
 		       struct regcache *regs,
-		       arm_displaced_step_closure *dsc)
+		       arm_displaced_step_copy_insn_closure *dsc)
 {
   int link = bit (insn, 7);
   unsigned int rm = bits (insn, 3, 6);
@@ -5152,7 +5152,7 @@ thumb_copy_bx_blx_reg (struct gdbarch *gdbarch, uint16_t insn,
 
 static void
 cleanup_alu_imm (struct gdbarch *gdbarch,
-		 struct regcache *regs, arm_displaced_step_closure *dsc)
+		 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
 {
   ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
   displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
@@ -5162,7 +5162,7 @@ cleanup_alu_imm (struct gdbarch *gdbarch,
 
 static int
 arm_copy_alu_imm (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
-		  arm_displaced_step_closure *dsc)
+		  arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int rn = bits (insn, 16, 19);
   unsigned int rd = bits (insn, 12, 15);
@@ -5210,7 +5210,7 @@ arm_copy_alu_imm (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
 static int
 thumb2_copy_alu_imm (struct gdbarch *gdbarch, uint16_t insn1,
 		     uint16_t insn2, struct regcache *regs,
-		     arm_displaced_step_closure *dsc)
+		     arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int op = bits (insn1, 5, 8);
   unsigned int rn, rm, rd;
@@ -5261,7 +5261,7 @@ thumb2_copy_alu_imm (struct gdbarch *gdbarch, uint16_t insn1,
 
 static void
 cleanup_alu_reg (struct gdbarch *gdbarch,
-		 struct regcache *regs, arm_displaced_step_closure *dsc)
+		 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
 {
   ULONGEST rd_val;
   int i;
@@ -5276,7 +5276,7 @@ cleanup_alu_reg (struct gdbarch *gdbarch,
 
 static void
 install_alu_reg (struct gdbarch *gdbarch, struct regcache *regs,
-		 arm_displaced_step_closure *dsc,
+		 arm_displaced_step_copy_insn_closure *dsc,
 		 unsigned int rd, unsigned int rn, unsigned int rm)
 {
   ULONGEST rd_val, rn_val, rm_val;
@@ -5309,7 +5309,7 @@ install_alu_reg (struct gdbarch *gdbarch, struct regcache *regs,
 
 static int
 arm_copy_alu_reg (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
-		  arm_displaced_step_closure *dsc)
+		  arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int op = bits (insn, 21, 24);
   int is_mov = (op == 0xd);
@@ -5333,7 +5333,7 @@ arm_copy_alu_reg (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
 static int
 thumb_copy_alu_reg (struct gdbarch *gdbarch, uint16_t insn,
 		    struct regcache *regs,
-		    arm_displaced_step_closure *dsc)
+		    arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned rm, rd;
 
@@ -5357,7 +5357,7 @@ thumb_copy_alu_reg (struct gdbarch *gdbarch, uint16_t insn,
 static void
 cleanup_alu_shifted_reg (struct gdbarch *gdbarch,
 			 struct regcache *regs,
-			 arm_displaced_step_closure *dsc)
+			 arm_displaced_step_copy_insn_closure *dsc)
 {
   ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
   int i;
@@ -5370,7 +5370,7 @@ cleanup_alu_shifted_reg (struct gdbarch *gdbarch,
 
 static void
 install_alu_shifted_reg (struct gdbarch *gdbarch, struct regcache *regs,
-			 arm_displaced_step_closure *dsc,
+			 arm_displaced_step_copy_insn_closure *dsc,
 			 unsigned int rd, unsigned int rn, unsigned int rm,
 			 unsigned rs)
 {
@@ -5409,7 +5409,7 @@ install_alu_shifted_reg (struct gdbarch *gdbarch, struct regcache *regs,
 static int
 arm_copy_alu_shifted_reg (struct gdbarch *gdbarch, uint32_t insn,
 			  struct regcache *regs,
-			  arm_displaced_step_closure *dsc)
+			  arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int op = bits (insn, 21, 24);
   int is_mov = (op == 0xd);
@@ -5441,7 +5441,7 @@ arm_copy_alu_shifted_reg (struct gdbarch *gdbarch, uint32_t insn,
 
 static void
 cleanup_load (struct gdbarch *gdbarch, struct regcache *regs,
-	      arm_displaced_step_closure *dsc)
+	      arm_displaced_step_copy_insn_closure *dsc)
 {
   ULONGEST rt_val, rt_val2 = 0, rn_val;
 
@@ -5470,7 +5470,7 @@ cleanup_load (struct gdbarch *gdbarch, struct regcache *regs,
 
 static void
 cleanup_store (struct gdbarch *gdbarch, struct regcache *regs,
-	       arm_displaced_step_closure *dsc)
+	       arm_displaced_step_copy_insn_closure *dsc)
 {
   ULONGEST rn_val = displaced_read_reg (regs, dsc, 2);
 
@@ -5493,7 +5493,7 @@ cleanup_store (struct gdbarch *gdbarch, struct regcache *regs,
 
 static int
 arm_copy_extra_ld_st (struct gdbarch *gdbarch, uint32_t insn, int unprivileged,
-		      struct regcache *regs, arm_displaced_step_closure *dsc)
+		      regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int op1 = bits (insn, 20, 24);
   unsigned int op2 = bits (insn, 5, 6);
@@ -5566,7 +5566,7 @@ arm_copy_extra_ld_st (struct gdbarch *gdbarch, uint32_t insn, int unprivileged,
 
 static void
 install_load_store (struct gdbarch *gdbarch, struct regcache *regs,
-		    arm_displaced_step_closure *dsc, int load,
+		    arm_displaced_step_copy_insn_closure *dsc, int load,
 		    int immed, int writeback, int size, int usermode,
 		    int rt, int rm, int rn)
 {
@@ -5622,7 +5622,7 @@ install_load_store (struct gdbarch *gdbarch, struct regcache *regs,
 static int
 thumb2_copy_load_literal (struct gdbarch *gdbarch, uint16_t insn1,
 			  uint16_t insn2, struct regcache *regs,
-			  arm_displaced_step_closure *dsc, int size)
+			  arm_displaced_step_copy_insn_closure *dsc, int size)
 {
   unsigned int u_bit = bit (insn1, 7);
   unsigned int rt = bits (insn2, 12, 15);
@@ -5676,7 +5676,7 @@ thumb2_copy_load_literal (struct gdbarch *gdbarch, uint16_t insn1,
 static int
 thumb2_copy_load_reg_imm (struct gdbarch *gdbarch, uint16_t insn1,
 			  uint16_t insn2, struct regcache *regs,
-			  arm_displaced_step_closure *dsc,
+			  arm_displaced_step_copy_insn_closure *dsc,
 			  int writeback, int immed)
 {
   unsigned int rt = bits (insn2, 12, 15);
@@ -5723,7 +5723,7 @@ thumb2_copy_load_reg_imm (struct gdbarch *gdbarch, uint16_t insn1,
 static int
 arm_copy_ldr_str_ldrb_strb (struct gdbarch *gdbarch, uint32_t insn,
 			    struct regcache *regs,
-			    arm_displaced_step_closure *dsc,
+			    arm_displaced_step_copy_insn_closure *dsc,
 			    int load, int size, int usermode)
 {
   int immed = !bit (insn, 25);
@@ -5806,7 +5806,7 @@ arm_copy_ldr_str_ldrb_strb (struct gdbarch *gdbarch, uint32_t insn,
 
 static void
 cleanup_block_load_all (struct gdbarch *gdbarch, struct regcache *regs,
-			arm_displaced_step_closure *dsc)
+			arm_displaced_step_copy_insn_closure *dsc)
 {
   int inc = dsc->u.block.increment;
   int bump_before = dsc->u.block.before ? (inc ? 4 : -4) : 0;
@@ -5866,7 +5866,7 @@ cleanup_block_load_all (struct gdbarch *gdbarch, struct regcache *regs,
 
 static void
 cleanup_block_store_pc (struct gdbarch *gdbarch, struct regcache *regs,
-			arm_displaced_step_closure *dsc)
+			arm_displaced_step_copy_insn_closure *dsc)
 {
   uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
   int store_executed = condition_true (dsc->u.block.cond, status);
@@ -5917,7 +5917,7 @@ cleanup_block_store_pc (struct gdbarch *gdbarch, struct regcache *regs,
 static void
 cleanup_block_load_pc (struct gdbarch *gdbarch,
 		       struct regcache *regs,
-		       arm_displaced_step_closure *dsc)
+		       arm_displaced_step_copy_insn_closure *dsc)
 {
   uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
   int load_executed = condition_true (dsc->u.block.cond, status);
@@ -5993,7 +5993,7 @@ cleanup_block_load_pc (struct gdbarch *gdbarch,
 static int
 arm_copy_block_xfer (struct gdbarch *gdbarch, uint32_t insn,
 		     struct regcache *regs,
-		     arm_displaced_step_closure *dsc)
+		     arm_displaced_step_copy_insn_closure *dsc)
 {
   int load = bit (insn, 20);
   int user = bit (insn, 22);
@@ -6105,7 +6105,7 @@ arm_copy_block_xfer (struct gdbarch *gdbarch, uint32_t insn,
 static int
 thumb2_copy_block_xfer (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
 			struct regcache *regs,
-			arm_displaced_step_closure *dsc)
+			arm_displaced_step_copy_insn_closure *dsc)
 {
   int rn = bits (insn1, 0, 3);
   int load = bit (insn1, 4);
@@ -6249,7 +6249,7 @@ arm_software_single_step (struct regcache *regcache)
 
 static void
 cleanup_svc (struct gdbarch *gdbarch, struct regcache *regs,
-	     arm_displaced_step_closure *dsc)
+	     arm_displaced_step_copy_insn_closure *dsc)
 {
   CORE_ADDR resume_addr = dsc->insn_addr + dsc->insn_size;
 
@@ -6264,7 +6264,7 @@ cleanup_svc (struct gdbarch *gdbarch, struct regcache *regs,
 
 static int
 install_svc (struct gdbarch *gdbarch, struct regcache *regs,
-	     arm_displaced_step_closure *dsc)
+	     arm_displaced_step_copy_insn_closure *dsc)
 {
   /* Preparation: none.
      Insn: unmodified svc.
@@ -6286,7 +6286,7 @@ install_svc (struct gdbarch *gdbarch, struct regcache *regs,
 
 static int
 arm_copy_svc (struct gdbarch *gdbarch, uint32_t insn,
-	      struct regcache *regs, arm_displaced_step_closure *dsc)
+	      regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
 {
 
   displaced_debug_printf ("copying svc insn %.8lx",
@@ -6299,7 +6299,7 @@ arm_copy_svc (struct gdbarch *gdbarch, uint32_t insn,
 
 static int
 thumb_copy_svc (struct gdbarch *gdbarch, uint16_t insn,
-		struct regcache *regs, arm_displaced_step_closure *dsc)
+		regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
 {
 
   displaced_debug_printf ("copying svc insn %.4x", insn);
@@ -6313,7 +6313,7 @@ thumb_copy_svc (struct gdbarch *gdbarch, uint16_t insn,
 
 static int
 arm_copy_undef (struct gdbarch *gdbarch, uint32_t insn,
-		arm_displaced_step_closure *dsc)
+		arm_displaced_step_copy_insn_closure *dsc)
 {
   displaced_debug_printf ("copying undefined insn %.8lx",
 			  (unsigned long) insn);
@@ -6325,7 +6325,7 @@ arm_copy_undef (struct gdbarch *gdbarch, uint32_t insn,
 
 static int
 thumb_32bit_copy_undef (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
-		       arm_displaced_step_closure *dsc)
+			arm_displaced_step_copy_insn_closure *dsc)
 {
 
   displaced_debug_printf ("copying undefined insn %.4x %.4x",
@@ -6342,7 +6342,7 @@ thumb_32bit_copy_undef (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
 
 static int
 arm_copy_unpred (struct gdbarch *gdbarch, uint32_t insn,
-		 arm_displaced_step_closure *dsc)
+		 arm_displaced_step_copy_insn_closure *dsc)
 {
   displaced_debug_printf ("copying unpredictable insn %.8lx",
 			  (unsigned long) insn);
@@ -6358,7 +6358,7 @@ arm_copy_unpred (struct gdbarch *gdbarch, uint32_t insn,
 static int
 arm_decode_misc_memhint_neon (struct gdbarch *gdbarch, uint32_t insn,
 			      struct regcache *regs,
-			      arm_displaced_step_closure *dsc)
+			      arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int op1 = bits (insn, 20, 26), op2 = bits (insn, 4, 7);
   unsigned int rn = bits (insn, 16, 19);
@@ -6418,7 +6418,7 @@ arm_decode_misc_memhint_neon (struct gdbarch *gdbarch, uint32_t insn,
 static int
 arm_decode_unconditional (struct gdbarch *gdbarch, uint32_t insn,
 			  struct regcache *regs,
-			  arm_displaced_step_closure *dsc)
+			  arm_displaced_step_copy_insn_closure *dsc)
 {
   if (bit (insn, 27) == 0)
     return arm_decode_misc_memhint_neon (gdbarch, insn, regs, dsc);
@@ -6503,7 +6503,7 @@ arm_decode_unconditional (struct gdbarch *gdbarch, uint32_t insn,
 static int
 arm_decode_miscellaneous (struct gdbarch *gdbarch, uint32_t insn,
 			  struct regcache *regs,
-			  arm_displaced_step_closure *dsc)
+			  arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int op2 = bits (insn, 4, 6);
   unsigned int op = bits (insn, 21, 22);
@@ -6554,7 +6554,7 @@ arm_decode_miscellaneous (struct gdbarch *gdbarch, uint32_t insn,
 static int
 arm_decode_dp_misc (struct gdbarch *gdbarch, uint32_t insn,
 		    struct regcache *regs,
-		    arm_displaced_step_closure *dsc)
+		    arm_displaced_step_copy_insn_closure *dsc)
 {
   if (bit (insn, 25))
     switch (bits (insn, 20, 24))
@@ -6600,7 +6600,7 @@ arm_decode_dp_misc (struct gdbarch *gdbarch, uint32_t insn,
 static int
 arm_decode_ld_st_word_ubyte (struct gdbarch *gdbarch, uint32_t insn,
 			     struct regcache *regs,
-			     arm_displaced_step_closure *dsc)
+			     arm_displaced_step_copy_insn_closure *dsc)
 {
   int a = bit (insn, 25), b = bit (insn, 4);
   uint32_t op1 = bits (insn, 20, 24);
@@ -6636,7 +6636,7 @@ arm_decode_ld_st_word_ubyte (struct gdbarch *gdbarch, uint32_t insn,
 
 static int
 arm_decode_media (struct gdbarch *gdbarch, uint32_t insn,
-		  arm_displaced_step_closure *dsc)
+		  arm_displaced_step_copy_insn_closure *dsc)
 {
   switch (bits (insn, 20, 24))
     {
@@ -6693,7 +6693,7 @@ arm_decode_media (struct gdbarch *gdbarch, uint32_t insn,
 static int
 arm_decode_b_bl_ldmstm (struct gdbarch *gdbarch, uint32_t insn,
 			struct regcache *regs,
-			arm_displaced_step_closure *dsc)
+			arm_displaced_step_copy_insn_closure *dsc)
 {
   if (bit (insn, 25))
     return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
@@ -6704,7 +6704,7 @@ arm_decode_b_bl_ldmstm (struct gdbarch *gdbarch, uint32_t insn,
 static int
 arm_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint32_t insn,
 			  struct regcache *regs,
-			  arm_displaced_step_closure *dsc)
+			  arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int opcode = bits (insn, 20, 24);
 
@@ -6737,7 +6737,7 @@ arm_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint32_t insn,
 static int
 thumb2_decode_dp_shift_reg (struct gdbarch *gdbarch, uint16_t insn1,
 			    uint16_t insn2,  struct regcache *regs,
-			    arm_displaced_step_closure *dsc)
+			    arm_displaced_step_copy_insn_closure *dsc)
 {
   /* PC is only allowed to be used in instruction MOV.  */
 
@@ -6758,7 +6758,7 @@ thumb2_decode_dp_shift_reg (struct gdbarch *gdbarch, uint16_t insn1,
 static int
 thumb2_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint16_t insn1,
 			     uint16_t insn2,  struct regcache *regs,
-			     arm_displaced_step_closure *dsc)
+			     arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int opcode = bits (insn1, 4, 8);
 
@@ -6793,7 +6793,7 @@ thumb2_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint16_t insn1,
 
 static int
 arm_decode_svc_copro (struct gdbarch *gdbarch, uint32_t insn,
-		      struct regcache *regs, arm_displaced_step_closure *dsc)
+		      regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int op1 = bits (insn, 20, 25);
   int op = bit (insn, 4);
@@ -6839,7 +6839,7 @@ arm_decode_svc_copro (struct gdbarch *gdbarch, uint32_t insn,
 static int
 thumb2_decode_svc_copro (struct gdbarch *gdbarch, uint16_t insn1,
 			 uint16_t insn2, struct regcache *regs,
-			 arm_displaced_step_closure *dsc)
+			 arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int coproc = bits (insn2, 8, 11);
   unsigned int bit_5_8 = bits (insn1, 5, 8);
@@ -6879,7 +6879,7 @@ thumb2_decode_svc_copro (struct gdbarch *gdbarch, uint16_t insn1,
 
 static void
 install_pc_relative (struct gdbarch *gdbarch, struct regcache *regs,
-		     arm_displaced_step_closure *dsc, int rd)
+		     arm_displaced_step_copy_insn_closure *dsc, int rd)
 {
   /* ADR Rd, #imm
 
@@ -6897,7 +6897,7 @@ install_pc_relative (struct gdbarch *gdbarch, struct regcache *regs,
 
 static int
 thumb_copy_pc_relative_16bit (struct gdbarch *gdbarch, struct regcache *regs,
-			      arm_displaced_step_closure *dsc,
+			      arm_displaced_step_copy_insn_closure *dsc,
 			      int rd, unsigned int imm)
 {
 
@@ -6912,7 +6912,7 @@ thumb_copy_pc_relative_16bit (struct gdbarch *gdbarch, struct regcache *regs,
 static int
 thumb_decode_pc_relative_16bit (struct gdbarch *gdbarch, uint16_t insn,
 				struct regcache *regs,
-				arm_displaced_step_closure *dsc)
+				arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int rd = bits (insn, 8, 10);
   unsigned int imm8 = bits (insn, 0, 7);
@@ -6926,7 +6926,7 @@ thumb_decode_pc_relative_16bit (struct gdbarch *gdbarch, uint16_t insn,
 static int
 thumb_copy_pc_relative_32bit (struct gdbarch *gdbarch, uint16_t insn1,
 			      uint16_t insn2, struct regcache *regs,
-			      arm_displaced_step_closure *dsc)
+			      arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int rd = bits (insn2, 8, 11);
   /* Since immediate has the same encoding in ADR ADD and SUB, so we simply
@@ -6961,7 +6961,7 @@ thumb_copy_pc_relative_32bit (struct gdbarch *gdbarch, uint16_t insn1,
 static int
 thumb_copy_16bit_ldr_literal (struct gdbarch *gdbarch, uint16_t insn1,
 			      struct regcache *regs,
-			      arm_displaced_step_closure *dsc)
+			      arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned int rt = bits (insn1, 8, 10);
   unsigned int pc;
@@ -7008,7 +7008,7 @@ thumb_copy_16bit_ldr_literal (struct gdbarch *gdbarch, uint16_t insn1,
 static int
 thumb_copy_cbnz_cbz (struct gdbarch *gdbarch, uint16_t insn1,
 		     struct regcache *regs,
-		     arm_displaced_step_closure *dsc)
+		     arm_displaced_step_copy_insn_closure *dsc)
 {
   int non_zero = bit (insn1, 11);
   unsigned int imm5 = (bit (insn1, 9) << 6) | (bits (insn1, 3, 7) << 1);
@@ -7045,7 +7045,7 @@ thumb_copy_cbnz_cbz (struct gdbarch *gdbarch, uint16_t insn1,
 static int
 thumb2_copy_table_branch (struct gdbarch *gdbarch, uint16_t insn1,
 			  uint16_t insn2, struct regcache *regs,
-			  arm_displaced_step_closure *dsc)
+			  arm_displaced_step_copy_insn_closure *dsc)
 {
   ULONGEST rn_val, rm_val;
   int is_tbh = bit (insn2, 4);
@@ -7087,7 +7087,7 @@ thumb2_copy_table_branch (struct gdbarch *gdbarch, uint16_t insn1,
 
 static void
 cleanup_pop_pc_16bit_all (struct gdbarch *gdbarch, struct regcache *regs,
-			  arm_displaced_step_closure *dsc)
+			  arm_displaced_step_copy_insn_closure *dsc)
 {
   /* PC <- r7 */
   int val = displaced_read_reg (regs, dsc, 7);
@@ -7105,7 +7105,7 @@ cleanup_pop_pc_16bit_all (struct gdbarch *gdbarch, struct regcache *regs,
 static int
 thumb_copy_pop_pc_16bit (struct gdbarch *gdbarch, uint16_t insn1,
 			 struct regcache *regs,
-			 arm_displaced_step_closure *dsc)
+			 arm_displaced_step_copy_insn_closure *dsc)
 {
   dsc->u.block.regmask = insn1 & 0x00ff;
 
@@ -7174,7 +7174,7 @@ thumb_copy_pop_pc_16bit (struct gdbarch *gdbarch, uint16_t insn1,
 static void
 thumb_process_displaced_16bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
 				    struct regcache *regs,
-				    arm_displaced_step_closure *dsc)
+				    arm_displaced_step_copy_insn_closure *dsc)
 {
   unsigned short op_bit_12_15 = bits (insn1, 12, 15);
   unsigned short op_bit_10_11 = bits (insn1, 10, 11);
@@ -7278,7 +7278,7 @@ static int
 decode_thumb_32bit_ld_mem_hints (struct gdbarch *gdbarch,
 				 uint16_t insn1, uint16_t insn2,
 				 struct regcache *regs,
-				 arm_displaced_step_closure *dsc)
+				 arm_displaced_step_copy_insn_closure *dsc)
 {
   int rt = bits (insn2, 12, 15);
   int rn = bits (insn1, 0, 3);
@@ -7357,7 +7357,7 @@ decode_thumb_32bit_ld_mem_hints (struct gdbarch *gdbarch,
 static void
 thumb_process_displaced_32bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
 				    uint16_t insn2, struct regcache *regs,
-				    arm_displaced_step_closure *dsc)
+				    arm_displaced_step_copy_insn_closure *dsc)
 {
   int err = 0;
   unsigned short op = bit (insn2, 15);
@@ -7485,7 +7485,7 @@ thumb_process_displaced_32bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
 static void
 thumb_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
 			      struct regcache *regs,
-			      arm_displaced_step_closure *dsc)
+			      arm_displaced_step_copy_insn_closure *dsc)
 {
   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
   uint16_t insn1
@@ -7509,7 +7509,7 @@ thumb_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
 void
 arm_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
 			    CORE_ADDR to, struct regcache *regs,
-			    arm_displaced_step_closure *dsc)
+			    arm_displaced_step_copy_insn_closure *dsc)
 {
   int err = 0;
   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
@@ -7566,7 +7566,8 @@ arm_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
 
 void
 arm_displaced_init_closure (struct gdbarch *gdbarch, CORE_ADDR from,
-			    CORE_ADDR to, arm_displaced_step_closure *dsc)
+			    CORE_ADDR to,
+			    arm_displaced_step_copy_insn_closure *dsc)
 {
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
   unsigned int i, len, offset;
@@ -7616,11 +7617,12 @@ arm_displaced_init_closure (struct gdbarch *gdbarch, CORE_ADDR from,
 
 void
 arm_displaced_step_fixup (struct gdbarch *gdbarch,
-			  struct displaced_step_closure *dsc_,
+			  struct displaced_step_copy_insn_closure *dsc_,
 			  CORE_ADDR from, CORE_ADDR to,
 			  struct regcache *regs)
 {
-  arm_displaced_step_closure *dsc = (arm_displaced_step_closure *) dsc_;
+  arm_displaced_step_copy_insn_closure *dsc
+    = (arm_displaced_step_copy_insn_closure *) dsc_;
 
   if (dsc->cleanup)
     dsc->cleanup (gdbarch, regs, dsc);
diff --git a/gdb/arm-tdep.h b/gdb/arm-tdep.h
index fd7e2ea7e76..b1f52ff229f 100644
--- a/gdb/arm-tdep.h
+++ b/gdb/arm-tdep.h
@@ -149,7 +149,8 @@ struct gdbarch_tdep
    sequence) and any scratch words, etc.  */
 #define ARM_DISPLACED_MODIFIED_INSNS	8
 
-struct arm_displaced_step_closure : public displaced_step_closure
+struct arm_displaced_step_copy_insn_closure
+  : public displaced_step_copy_insn_closure
 {
   ULONGEST tmp[DISPLACED_TEMPS];
   int rd;
@@ -196,7 +197,7 @@ struct arm_displaced_step_closure : public displaced_step_closure
       /* If non-NULL, override generic SVC handling (e.g. for a particular
 	 OS).  */
       int (*copy_svc_os) (struct gdbarch *gdbarch, struct regcache *regs,
-			  arm_displaced_step_closure *dsc);
+			  arm_displaced_step_copy_insn_closure *dsc);
     } svc;
   } u;
 
@@ -215,7 +216,7 @@ struct arm_displaced_step_closure : public displaced_step_closure
   CORE_ADDR insn_addr;
   CORE_ADDR scratch_base;
   void (*cleanup) (struct gdbarch *, struct regcache *,
-		   arm_displaced_step_closure *);
+		   arm_displaced_step_copy_insn_closure *);
 };
 
 /* Values for the WRITE_PC argument to displaced_write_reg.  If the register
@@ -234,16 +235,17 @@ enum pc_write_style
 extern void
   arm_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
 			      CORE_ADDR to, struct regcache *regs,
-			      arm_displaced_step_closure *dsc);
+			      arm_displaced_step_copy_insn_closure *dsc);
 extern void
   arm_displaced_init_closure (struct gdbarch *gdbarch, CORE_ADDR from,
-			      CORE_ADDR to, arm_displaced_step_closure *dsc);
+			      CORE_ADDR to,
+			      arm_displaced_step_copy_insn_closure *dsc);
 extern ULONGEST
-  displaced_read_reg (struct regcache *regs, arm_displaced_step_closure *dsc,
+  displaced_read_reg (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
 		      int regno);
 extern void
   displaced_write_reg (struct regcache *regs,
-		       arm_displaced_step_closure *dsc, int regno,
+		       arm_displaced_step_copy_insn_closure *dsc, int regno,
 		       ULONGEST val, enum pc_write_style write_pc);
 
 CORE_ADDR arm_skip_stub (struct frame_info *, CORE_ADDR);
@@ -262,7 +264,7 @@ int arm_is_thumb (struct regcache *regcache);
 int arm_frame_is_thumb (struct frame_info *frame);
 
 extern void arm_displaced_step_fixup (struct gdbarch *,
-				      struct displaced_step_closure *,
+				      displaced_step_copy_insn_closure *,
 				      CORE_ADDR, CORE_ADDR, struct regcache *);
 
 /* Return the bit mask in ARM_PS_REGNUM that indicates Thumb mode.  */
diff --git a/gdb/gdbarch.c b/gdb/gdbarch.c
index 0b43ae434ea..3da77416d6e 100644
--- a/gdb/gdbarch.c
+++ b/gdb/gdbarch.c
@@ -3969,7 +3969,7 @@ gdbarch_displaced_step_copy_insn_p (struct gdbarch *gdbarch)
   return gdbarch->displaced_step_copy_insn != NULL;
 }
 
-displaced_step_closure_up
+displaced_step_copy_insn_closure_up
 gdbarch_displaced_step_copy_insn (struct gdbarch *gdbarch, CORE_ADDR from, CORE_ADDR to, struct regcache *regs)
 {
   gdb_assert (gdbarch != NULL);
@@ -4011,7 +4011,7 @@ gdbarch_displaced_step_fixup_p (struct gdbarch *gdbarch)
 }
 
 void
-gdbarch_displaced_step_fixup (struct gdbarch *gdbarch, struct displaced_step_closure *closure, CORE_ADDR from, CORE_ADDR to, struct regcache *regs)
+gdbarch_displaced_step_fixup (struct gdbarch *gdbarch, struct displaced_step_copy_insn_closure *closure, CORE_ADDR from, CORE_ADDR to, struct regcache *regs)
 {
   gdb_assert (gdbarch != NULL);
   gdb_assert (gdbarch->displaced_step_fixup != NULL);
diff --git a/gdb/gdbarch.h b/gdb/gdbarch.h
index b620bb25b90..0e4e4da0c1b 100644
--- a/gdb/gdbarch.h
+++ b/gdb/gdbarch.h
@@ -1030,8 +1030,8 @@ extern void set_gdbarch_max_insn_length (struct gdbarch *gdbarch, ULONGEST max_i
 
 extern bool gdbarch_displaced_step_copy_insn_p (struct gdbarch *gdbarch);
 
-typedef displaced_step_closure_up (gdbarch_displaced_step_copy_insn_ftype) (struct gdbarch *gdbarch, CORE_ADDR from, CORE_ADDR to, struct regcache *regs);
-extern displaced_step_closure_up gdbarch_displaced_step_copy_insn (struct gdbarch *gdbarch, CORE_ADDR from, CORE_ADDR to, struct regcache *regs);
+typedef displaced_step_copy_insn_closure_up (gdbarch_displaced_step_copy_insn_ftype) (struct gdbarch *gdbarch, CORE_ADDR from, CORE_ADDR to, struct regcache *regs);
+extern displaced_step_copy_insn_closure_up gdbarch_displaced_step_copy_insn (struct gdbarch *gdbarch, CORE_ADDR from, CORE_ADDR to, struct regcache *regs);
 extern void set_gdbarch_displaced_step_copy_insn (struct gdbarch *gdbarch, gdbarch_displaced_step_copy_insn_ftype *displaced_step_copy_insn);
 
 /* Return true if GDB should use hardware single-stepping to execute a displaced
@@ -1066,8 +1066,8 @@ extern void set_gdbarch_displaced_step_hw_singlestep (struct gdbarch *gdbarch, g
 
 extern bool gdbarch_displaced_step_fixup_p (struct gdbarch *gdbarch);
 
-typedef void (gdbarch_displaced_step_fixup_ftype) (struct gdbarch *gdbarch, struct displaced_step_closure *closure, CORE_ADDR from, CORE_ADDR to, struct regcache *regs);
-extern void gdbarch_displaced_step_fixup (struct gdbarch *gdbarch, struct displaced_step_closure *closure, CORE_ADDR from, CORE_ADDR to, struct regcache *regs);
+typedef void (gdbarch_displaced_step_fixup_ftype) (struct gdbarch *gdbarch, struct displaced_step_copy_insn_closure *closure, CORE_ADDR from, CORE_ADDR to, struct regcache *regs);
+extern void gdbarch_displaced_step_fixup (struct gdbarch *gdbarch, struct displaced_step_copy_insn_closure *closure, CORE_ADDR from, CORE_ADDR to, struct regcache *regs);
 extern void set_gdbarch_displaced_step_fixup (struct gdbarch *gdbarch, gdbarch_displaced_step_fixup_ftype *displaced_step_fixup);
 
 /* Return the address of an appropriate place to put displaced
diff --git a/gdb/gdbarch.sh b/gdb/gdbarch.sh
index d27318a757c..d096b7bffec 100755
--- a/gdb/gdbarch.sh
+++ b/gdb/gdbarch.sh
@@ -783,7 +783,7 @@ V;ULONGEST;max_insn_length;;;0;0
 # If the instruction cannot execute out of line, return NULL.  The
 # core falls back to stepping past the instruction in-line instead in
 # that case.
-M;displaced_step_closure_up;displaced_step_copy_insn;CORE_ADDR from, CORE_ADDR to, struct regcache *regs;from, to, regs
+M;displaced_step_copy_insn_closure_up;displaced_step_copy_insn;CORE_ADDR from, CORE_ADDR to, struct regcache *regs;from, to, regs
 
 # Return true if GDB should use hardware single-stepping to execute a displaced
 # step instruction.  If false, GDB will simply restart execution at the
@@ -811,7 +811,7 @@ m;bool;displaced_step_hw_singlestep;void;;;default_displaced_step_hw_singlestep;
 #
 # For a general explanation of displaced stepping and how GDB uses it,
 # see the comments in infrun.c.
-M;void;displaced_step_fixup;struct displaced_step_closure *closure, CORE_ADDR from, CORE_ADDR to, struct regcache *regs;closure, from, to, regs;;NULL
+M;void;displaced_step_fixup;struct displaced_step_copy_insn_closure *closure, CORE_ADDR from, CORE_ADDR to, struct regcache *regs;closure, from, to, regs;;NULL
 
 # Return the address of an appropriate place to put displaced
 # instructions while we step over them.  There need only be one such
diff --git a/gdb/i386-linux-tdep.c b/gdb/i386-linux-tdep.c
index b9ec9580634..ab7d23611f8 100644
--- a/gdb/i386-linux-tdep.c
+++ b/gdb/i386-linux-tdep.c
@@ -789,29 +789,30 @@ i386_linux_iterate_over_regset_sections (struct gdbarch *gdbarch,
    PC should get relocated back to its vDSO address.  Hide the 'ret'
    instruction by 'nop' so that i386_displaced_step_fixup is not confused.
    
-   It is not fully correct as the bytes in struct displaced_step_closure will
-   not match the inferior code.  But we would need some new flag in
-   displaced_step_closure otherwise to keep the state that syscall is finishing
-   for the later i386_displaced_step_fixup execution as the syscall execution
-   is already no longer detectable there.  The new flag field would mean
-   i386-linux-tdep.c needs to wrap all the displacement methods of i386-tdep.c
-   which does not seem worth it.  The same effect is achieved by patching that
-   'nop' instruction there instead.  */
-
-static displaced_step_closure_up
+   It is not fully correct as the bytes in struct
+   displaced_step_copy_insn_closure will not match the inferior code.  But we
+   would need some new flag in displaced_step_copy_insn_closure otherwise to
+   keep the state that syscall is finishing for the later
+   i386_displaced_step_fixup execution as the syscall execution is already no
+   longer detectable there.  The new flag field would mean i386-linux-tdep.c
+   needs to wrap all the displacement methods of i386-tdep.c which does not seem
+   worth it.  The same effect is achieved by patching that 'nop' instruction
+   there instead.  */
+
+static displaced_step_copy_insn_closure_up
 i386_linux_displaced_step_copy_insn (struct gdbarch *gdbarch,
 				     CORE_ADDR from, CORE_ADDR to,
 				     struct regcache *regs)
 {
-  displaced_step_closure_up closure_
+  displaced_step_copy_insn_closure_up closure_
     =  i386_displaced_step_copy_insn (gdbarch, from, to, regs);
 
   if (i386_linux_get_syscall_number_from_regcache (regs) != -1)
     {
       /* The closure returned by i386_displaced_step_copy_insn is simply a
 	 buffer with a copy of the instruction. */
-      i386_displaced_step_closure *closure
-	= (i386_displaced_step_closure *) closure_.get ();
+      i386_displaced_step_copy_insn_closure *closure
+	= (i386_displaced_step_copy_insn_closure *) closure_.get ();
 
       /* Fake nop.  */
       closure->buf[0] = 0x90;
diff --git a/gdb/i386-tdep.c b/gdb/i386-tdep.c
index 00de4e1ccb9..42918b0b697 100644
--- a/gdb/i386-tdep.c
+++ b/gdb/i386-tdep.c
@@ -799,14 +799,14 @@ i386_insn_is_jump (struct gdbarch *gdbarch, CORE_ADDR addr)
 
 /* Some kernels may run one past a syscall insn, so we have to cope.  */
 
-displaced_step_closure_up
+displaced_step_copy_insn_closure_up
 i386_displaced_step_copy_insn (struct gdbarch *gdbarch,
 			       CORE_ADDR from, CORE_ADDR to,
 			       struct regcache *regs)
 {
   size_t len = gdbarch_max_insn_length (gdbarch);
-  std::unique_ptr<i386_displaced_step_closure> closure
-    (new i386_displaced_step_closure (len));
+  std::unique_ptr<i386_displaced_step_copy_insn_closure> closure
+    (new i386_displaced_step_copy_insn_closure (len));
   gdb_byte *buf = closure->buf.data ();
 
   read_memory (from, buf, len);
@@ -830,7 +830,7 @@ i386_displaced_step_copy_insn (struct gdbarch *gdbarch,
 			  displaced_step_dump_bytes (buf, len).c_str ());
 
   /* This is a work around for a problem with g++ 4.8.  */
-  return displaced_step_closure_up (closure.release ());
+  return displaced_step_copy_insn_closure_up (closure.release ());
 }
 
 /* Fix up the state of registers and memory after having single-stepped
@@ -838,7 +838,7 @@ i386_displaced_step_copy_insn (struct gdbarch *gdbarch,
 
 void
 i386_displaced_step_fixup (struct gdbarch *gdbarch,
-			   struct displaced_step_closure *closure_,
+			   struct displaced_step_copy_insn_closure *closure_,
 			   CORE_ADDR from, CORE_ADDR to,
 			   struct regcache *regs)
 {
@@ -850,8 +850,8 @@ i386_displaced_step_fixup (struct gdbarch *gdbarch,
      applying it.  */
   ULONGEST insn_offset = to - from;
 
-  i386_displaced_step_closure *closure
-    = (i386_displaced_step_closure *) closure_;
+  i386_displaced_step_copy_insn_closure *closure
+    = (i386_displaced_step_copy_insn_closure *) closure_;
   gdb_byte *insn = closure->buf.data ();
   /* The start of the insn, needed in case we see some prefixes.  */
   gdb_byte *insn_start = insn;
diff --git a/gdb/i386-tdep.h b/gdb/i386-tdep.h
index 45d64eb009e..f8a93b121c9 100644
--- a/gdb/i386-tdep.h
+++ b/gdb/i386-tdep.h
@@ -439,15 +439,15 @@ extern void
 				     void *cb_data,
 				     const struct regcache *regcache);
 
-typedef buf_displaced_step_closure i386_displaced_step_closure;
+typedef buf_displaced_step_copy_insn_closure
+  i386_displaced_step_copy_insn_closure;
 
-extern displaced_step_closure_up i386_displaced_step_copy_insn
+extern displaced_step_copy_insn_closure_up i386_displaced_step_copy_insn
   (struct gdbarch *gdbarch, CORE_ADDR from, CORE_ADDR to,
    struct regcache *regs);
-extern void i386_displaced_step_fixup (struct gdbarch *gdbarch,
-				       struct displaced_step_closure *closure,
-				       CORE_ADDR from, CORE_ADDR to,
-				       struct regcache *regs);
+extern void i386_displaced_step_fixup
+  (struct gdbarch *gdbarch, displaced_step_copy_insn_closure *closure,
+   CORE_ADDR from, CORE_ADDR to, regcache *regs);
 
 /* Initialize a basic ELF architecture variant.  */
 extern void i386_elf_init_abi (struct gdbarch_info, struct gdbarch *);
diff --git a/gdb/infrun.c b/gdb/infrun.c
index 917a35c23ab..71e0ab1e4fb 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -1459,9 +1459,10 @@ step_over_info_valid_p (void)
    displaced step operation on it.  See displaced_step_prepare and
    displaced_step_fixup for details.  */
 
-/* Default destructor for displaced_step_closure.  */
+/* Default destructor for displaced_step_copy_insn_closure.  */
 
-displaced_step_closure::~displaced_step_closure () = default;
+displaced_step_copy_insn_closure::~displaced_step_copy_insn_closure ()
+  = default;
 
 /* Get the displaced stepping state of inferior INF.  */
 
@@ -1505,11 +1506,11 @@ displaced_step_in_progress (inferior *inf)
 }
 
 /* If inferior is in displaced stepping, and ADDR equals to starting address
-   of copy area, return corresponding displaced_step_closure.  Otherwise,
-   return NULL.  */
+   of copy area, return corresponding displaced_step_copy_insn_closure.
+   Otherwise, return NULL.  */
 
-struct displaced_step_closure*
-get_displaced_step_closure_by_addr (CORE_ADDR addr)
+displaced_step_copy_insn_closure *
+get_displaced_step_copy_insn_closure_by_addr (CORE_ADDR addr)
 {
   displaced_step_inferior_state *displaced
     = get_displaced_stepping_state (current_inferior ());
diff --git a/gdb/infrun.h b/gdb/infrun.h
index ca0774e8e6b..a276ddf0250 100644
--- a/gdb/infrun.h
+++ b/gdb/infrun.h
@@ -239,8 +239,8 @@ extern void clear_exit_convenience_vars (void);
 /* Dump LEN bytes at BUF in hex to a string and return it.  */
 extern std::string displaced_step_dump_bytes (const gdb_byte *buf, size_t len);
 
-extern struct displaced_step_closure *get_displaced_step_closure_by_addr
-    (CORE_ADDR addr);
+extern struct displaced_step_copy_insn_closure *
+  get_displaced_step_copy_insn_closure_by_addr (CORE_ADDR addr);
 
 extern void update_observer_mode (void);
 
@@ -282,18 +282,19 @@ extern void all_uis_on_sync_execution_starting (void);
 
 /* Base class for displaced stepping closures (the arch-specific data).  */
 
-struct displaced_step_closure
+struct displaced_step_copy_insn_closure
 {
-  virtual ~displaced_step_closure () = 0;
+  virtual ~displaced_step_copy_insn_closure () = 0;
 };
 
-using displaced_step_closure_up = std::unique_ptr<displaced_step_closure>;
+using displaced_step_copy_insn_closure_up
+  = std::unique_ptr<displaced_step_copy_insn_closure>;
 
 /* A simple displaced step closure that contains only a byte buffer.  */
 
-struct buf_displaced_step_closure : displaced_step_closure
+struct buf_displaced_step_copy_insn_closure : displaced_step_copy_insn_closure
 {
-  buf_displaced_step_closure (int buf_size)
+  buf_displaced_step_copy_insn_closure (int buf_size)
   : buf (buf_size)
   {}
 
@@ -334,7 +335,7 @@ struct displaced_step_inferior_state
 
   /* The closure provided gdbarch_displaced_step_copy_insn, to be used
      for post-step cleanup.  */
-  displaced_step_closure_up step_closure;
+  displaced_step_copy_insn_closure_up step_closure;
 
   /* The address of the original instruction, and the copy we
      made.  */
diff --git a/gdb/rs6000-tdep.c b/gdb/rs6000-tdep.c
index bde3ee7b357..626b47e244f 100644
--- a/gdb/rs6000-tdep.c
+++ b/gdb/rs6000-tdep.c
@@ -851,18 +851,19 @@ typedef BP_MANIPULATION_ENDIAN (little_breakpoint, big_breakpoint)
 					 || (insn & STORE_CONDITIONAL_MASK) == STHCX_INSTRUCTION \
 					 || (insn & STORE_CONDITIONAL_MASK) == STQCX_INSTRUCTION)
 
-typedef buf_displaced_step_closure ppc_displaced_step_closure;
+typedef buf_displaced_step_copy_insn_closure
+  ppc_displaced_step_copy_insn_closure;
 
 /* We can't displaced step atomic sequences.  */
 
-static displaced_step_closure_up
+static displaced_step_copy_insn_closure_up
 ppc_displaced_step_copy_insn (struct gdbarch *gdbarch,
 			      CORE_ADDR from, CORE_ADDR to,
 			      struct regcache *regs)
 {
   size_t len = gdbarch_max_insn_length (gdbarch);
-  std::unique_ptr<ppc_displaced_step_closure> closure
-    (new ppc_displaced_step_closure (len));
+  std::unique_ptr<ppc_displaced_step_copy_insn_closure> closure
+    (new ppc_displaced_step_copy_insn_closure (len));
   gdb_byte *buf = closure->buf.data ();
   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
   int insn;
@@ -887,20 +888,21 @@ ppc_displaced_step_copy_insn (struct gdbarch *gdbarch,
 			  displaced_step_dump_bytes (buf, len).c_str ());;
 
   /* This is a work around for a problem with g++ 4.8.  */
-  return displaced_step_closure_up (closure.release ());
+  return displaced_step_copy_insn_closure_up (closure.release ());
 }
 
 /* Fix up the state of registers and memory after having single-stepped
    a displaced instruction.  */
 static void
 ppc_displaced_step_fixup (struct gdbarch *gdbarch,
-			  struct displaced_step_closure *closure_,
+			  struct displaced_step_copy_insn_closure *closure_,
 			  CORE_ADDR from, CORE_ADDR to,
 			  struct regcache *regs)
 {
   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
   /* Our closure is a copy of the instruction.  */
-  ppc_displaced_step_closure *closure = (ppc_displaced_step_closure *) closure_;
+  ppc_displaced_step_copy_insn_closure *closure
+    = (ppc_displaced_step_copy_insn_closure *) closure_;
   ULONGEST insn  = extract_unsigned_integer (closure->buf.data (),
 					     PPC_INSN_SIZE, byte_order);
   ULONGEST opcode = 0;
diff --git a/gdb/s390-tdep.c b/gdb/s390-tdep.c
index ce013458e89..f1af2d20c4a 100644
--- a/gdb/s390-tdep.c
+++ b/gdb/s390-tdep.c
@@ -421,18 +421,19 @@ is_non_branch_ril (gdb_byte *insn)
   return 0;
 }
 
-typedef buf_displaced_step_closure s390_displaced_step_closure;
+typedef buf_displaced_step_copy_insn_closure
+  s390_displaced_step_copy_insn_closure;
 
 /* Implementation of gdbarch_displaced_step_copy_insn.  */
 
-static displaced_step_closure_up
+static displaced_step_copy_insn_closure_up
 s390_displaced_step_copy_insn (struct gdbarch *gdbarch,
 			       CORE_ADDR from, CORE_ADDR to,
 			       struct regcache *regs)
 {
   size_t len = gdbarch_max_insn_length (gdbarch);
-  std::unique_ptr<s390_displaced_step_closure> closure
-    (new s390_displaced_step_closure (len));
+  std::unique_ptr<s390_displaced_step_copy_insn_closure> closure
+    (new s390_displaced_step_copy_insn_closure (len));
   gdb_byte *buf = closure->buf.data ();
 
   read_memory (from, buf, len);
@@ -470,7 +471,7 @@ s390_displaced_step_copy_insn (struct gdbarch *gdbarch,
 			  displaced_step_dump_bytes (buf, len).c_str ());
 
   /* This is a work around for a problem with g++ 4.8.  */
-  return displaced_step_closure_up (closure.release ());
+  return displaced_step_copy_insn_closure_up (closure.release ());
 }
 
 /* Fix up the state of registers and memory after having single-stepped
@@ -478,13 +479,13 @@ s390_displaced_step_copy_insn (struct gdbarch *gdbarch,
 
 static void
 s390_displaced_step_fixup (struct gdbarch *gdbarch,
-			   struct displaced_step_closure *closure_,
+			   displaced_step_copy_insn_closure *closure_,
 			   CORE_ADDR from, CORE_ADDR to,
 			   struct regcache *regs)
 {
   /* Our closure is a copy of the instruction.  */
-  s390_displaced_step_closure *closure
-    = (s390_displaced_step_closure *) closure_;
+  s390_displaced_step_copy_insn_closure *closure
+    = (s390_displaced_step_copy_insn_closure *) closure_;
   gdb_byte *insn = closure->buf.data ();
   static int s390_instrlen[] = { 2, 4, 4, 6 };
   int insnlen = s390_instrlen[insn[0] >> 6];
-- 
2.28.0


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

* [PATCH 05/12] gdb: rename displaced_step_fixup to displaced_step_finish
  2020-11-10 21:46 [PATCH 00/12] Concurrent displaced stepping Simon Marchi
                   ` (3 preceding siblings ...)
  2020-11-10 21:46 ` [PATCH 04/12] gdb: rename displaced_step_closure to displaced_step_copy_insn_closure Simon Marchi
@ 2020-11-10 21:46 ` Simon Marchi
  2020-11-25  1:29   ` Pedro Alves
  2020-11-10 21:46 ` [PATCH 06/12] gdb: introduce status enum for displaced step prepare/finish Simon Marchi
                   ` (6 subsequent siblings)
  11 siblings, 1 reply; 44+ messages in thread
From: Simon Marchi @ 2020-11-10 21:46 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

This is a preparatory patch to reduce a little bit the diff size of the
main patch later in this series.  It renames the displaced_step_fixup
function in infrun.c to displaced_step_finish.

The rationale is to better differentiate the low and high level
operations.

We first have the low level operation of writing an instruction to a
displaced buffer, called "copy_insn".  The mirror low level operation to
fix up the state after having executed the instruction is "fixup".  The
high level operation of preparing a thread for a displaced step (which
includes doing the "copy_insn" and some more bookkeeping) is called
"prepare" (as in displaced_step_prepare).  The mirror high level
operation to cleaning up after a displaced step (which includes doing
the "fixup" and some more bookkeeping) is currently also called "fixup"
(as in displaced_step_fixup), just like the low level operation.

I think that choosing a different name for the low and high level
cleanup operation makes it clearer, hence "finish".

gdb/ChangeLog:

	* infrun.c (displaced_step_fixup): Rename to...
	(displaced_step_finish): ... this, update all callers.

Change-Id: Id32f48c1e2091d09854c77fcedcc14d2519957a2
---
 gdb/infrun.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/gdb/infrun.c b/gdb/infrun.c
index 71e0ab1e4fb..cdf198bb307 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -1457,7 +1457,7 @@ step_over_info_valid_p (void)
    place it in the displaced_step_request_queue.  Whenever a displaced
    step finishes, we pick the next thread in the queue and start a new
    displaced step operation on it.  See displaced_step_prepare and
-   displaced_step_fixup for details.  */
+   displaced_step_finish for details.  */
 
 /* Default destructor for displaced_step_copy_insn_closure.  */
 
@@ -1847,7 +1847,7 @@ displaced_step_restore (struct displaced_step_inferior_state *displaced,
    -1.  If the thread wasn't displaced stepping, return 0.  */
 
 static int
-displaced_step_fixup (thread_info *event_thread, enum gdb_signal signal)
+displaced_step_finish (thread_info *event_thread, enum gdb_signal signal)
 {
   struct displaced_step_inferior_state *displaced
     = get_displaced_stepping_state (event_thread->inf);
@@ -2208,7 +2208,7 @@ do_target_resume (ptid_t resume_ptid, bool step, enum gdb_signal sig)
 
      Likewise if we're displaced stepping, otherwise a trap for a
      breakpoint in a signal handler might be confused with the
-     displaced step finishing.  We don't make the displaced_step_fixup
+     displaced step finishing.  We don't make the displaced_step_finish
      step distinguish the cases instead, because:
 
      - a backtrace while stopped in the signal handler would show the
@@ -4822,7 +4822,7 @@ stop_all_threads (void)
 		      t->suspend.waitstatus.kind = TARGET_WAITKIND_IGNORE;
 		      t->suspend.waitstatus_pending_p = 0;
 
-		      if (displaced_step_fixup (t, GDB_SIGNAL_0) < 0)
+		      if (displaced_step_finish (t, GDB_SIGNAL_0) < 0)
 			{
 			  /* Add it back to the step-over queue.  */
 			  infrun_debug_printf
@@ -4850,7 +4850,7 @@ stop_all_threads (void)
 		      sig = (event.ws.kind == TARGET_WAITKIND_STOPPED
 			     ? event.ws.value.sig : GDB_SIGNAL_0);
 
-		      if (displaced_step_fixup (t, sig) < 0)
+		      if (displaced_step_finish (t, sig) < 0)
 			{
 			  /* Add it back to the step-over queue.  */
 			  t->control.trap_expected = 0;
@@ -5318,7 +5318,7 @@ handle_inferior_event (struct execution_control_state *ecs)
 	       has been done.  Perform cleanup for parent process here.  Note
 	       that this operation also cleans up the child process for vfork,
 	       because their pages are shared.  */
-	    displaced_step_fixup (ecs->event_thread, GDB_SIGNAL_TRAP);
+	    displaced_step_finish (ecs->event_thread, GDB_SIGNAL_TRAP);
 	    /* Start a new step-over in another thread if there's one
 	       that needs it.  */
 	    start_step_over ();
@@ -5661,8 +5661,8 @@ resumed_thread_with_pending_status (struct thread_info *tp,
 static int
 finish_step_over (struct execution_control_state *ecs)
 {
-  displaced_step_fixup (ecs->event_thread,
-			ecs->event_thread->suspend.stop_signal);
+  displaced_step_finish (ecs->event_thread,
+			 ecs->event_thread->suspend.stop_signal);
 
   bool had_step_over_info = step_over_info_valid_p ();
 
-- 
2.28.0


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

* [PATCH 06/12] gdb: introduce status enum for displaced step prepare/finish
  2020-11-10 21:46 [PATCH 00/12] Concurrent displaced stepping Simon Marchi
                   ` (4 preceding siblings ...)
  2020-11-10 21:46 ` [PATCH 05/12] gdb: rename displaced_step_fixup to displaced_step_finish Simon Marchi
@ 2020-11-10 21:46 ` Simon Marchi
  2020-11-11 23:36   ` Andrew Burgess
  2020-11-25  1:30   ` Pedro Alves
  2020-11-10 21:46 ` [PATCH 07/12] gdb: pass inferior to get_linux_inferior_data Simon Marchi
                   ` (5 subsequent siblings)
  11 siblings, 2 replies; 44+ messages in thread
From: Simon Marchi @ 2020-11-10 21:46 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

This is a preparatory patch to reduce the size of the diff of the
upcoming main patch.  It introduces enum types for the return values of
displaced step "prepare" and "finish" operations.  I find that this
expresses better the intention of the code, rather than returning
arbitrary integer values (-1, 0 and 1) which are difficult to remember.
That makes the code easier to read.

I put the new enum types in a new displaced-stepping.h file, because I
introduce that file in a later patch anyway.  Putting it there avoids
having to move it later.

There is one change in behavior for displaced_step_finish: it currently
returns 0 if the thread wasn't doing a displaced step and 1 if the
thread was doing a displaced step which was executed successfully.  It
turns out that this distinction is not needed by any caller, so I've
merged these two cases into "_OK", rather than adding an extra
enumerator.

gdb/ChangeLog:

	* infrun.c (displaced_step_prepare_throw): Change return type to
	displaced_step_prepare_status.
	(displaced_step_prepare): Likewise.
	(displaced_step_finish): Change return type to
	displaced_step_finish_status.
	(resume_1): Adjust.
	(stop_all_threads): Adjust.
	* displaced-stepping.h: New file.

Change-Id: I5c8fe07212cd398d5b486b5936d9d0807acd3788
---
 gdb/displaced-stepping.h | 46 +++++++++++++++++++++++++
 gdb/infrun.c             | 72 +++++++++++++++++++++++-----------------
 2 files changed, 88 insertions(+), 30 deletions(-)
 create mode 100644 gdb/displaced-stepping.h

diff --git a/gdb/displaced-stepping.h b/gdb/displaced-stepping.h
new file mode 100644
index 00000000000..9c7e85c3769
--- /dev/null
+++ b/gdb/displaced-stepping.h
@@ -0,0 +1,46 @@
+/* Displaced stepping related things.
+
+   Copyright (C) 2020 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#ifndef DISPLACED_STEPPING_H
+#define DISPLACED_STEPPING_H
+
+enum displaced_step_prepare_status
+{
+  /* A displaced stepping buffer was successfully allocated and prepared.  */
+  DISPLACED_STEP_PREPARE_STATUS_OK,
+
+  /* Something bad happened.  */
+  DISPLACED_STEP_PREPARE_STATUS_ERROR,
+
+  /* Not enough resources are available at this time, try again later.  */
+  DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE,
+};
+
+enum displaced_step_finish_status
+{
+  /* Either the instruction was stepped and fixed up, or the specified thread
+     wasn't executing a displaced step (in which case there's nothing to
+     finish). */
+  DISPLACED_STEP_FINISH_STATUS_OK,
+
+  /* The thread was executing a displaced step, but didn't complete it.  */
+  DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED,
+};
+
+#endif /* DISPLACED_STEPPING_H */
diff --git a/gdb/infrun.c b/gdb/infrun.c
index cdf198bb307..ed54c4331d7 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -19,6 +19,7 @@
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include "defs.h"
+#include "displaced-stepping.h"
 #include "infrun.h"
 #include <ctype.h>
 #include "symtab.h"
@@ -1652,11 +1653,13 @@ displaced_step_dump_bytes (const gdb_byte *buf, size_t len)
    Comments in the code for 'random signals' in handle_inferior_event
    explain how we handle this case instead.
 
-   Returns 1 if preparing was successful -- this thread is going to be
-   stepped now; 0 if displaced stepping this thread got queued; or -1
-   if this instruction can't be displaced stepped.  */
+   Returns DISPLACED_STEP_PREPARE_STATUS_OK if preparing was successful -- this
+   thread is going to be stepped now; DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE
+   if displaced stepping this thread got queued; or
+   DISPLACED_STEP_PREPARE_STATUS_ERROR if this instruction can't be displaced
+   stepped.  */
 
-static int
+static displaced_step_prepare_status
 displaced_step_prepare_throw (thread_info *tp)
 {
   regcache *regcache = get_thread_regcache (tp);
@@ -1694,7 +1697,7 @@ displaced_step_prepare_throw (thread_info *tp)
 			      target_pid_to_str (tp->ptid).c_str ());
 
       global_thread_step_over_chain_enqueue (tp);
-      return 0;
+      return DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE;
     }
   else
     displaced_debug_printf ("stepping %s now",
@@ -1725,7 +1728,7 @@ displaced_step_prepare_throw (thread_info *tp)
       displaced_debug_printf ("breakpoint set in scratch pad.  "
 			      "Stepping over breakpoint in-line instead.");
 
-      return -1;
+      return DISPLACED_STEP_PREPARE_STATUS_ERROR;
     }
 
   /* Save the original contents of the copy area.  */
@@ -1749,7 +1752,7 @@ displaced_step_prepare_throw (thread_info *tp)
       /* The architecture doesn't know how or want to displaced step
 	 this instruction or instruction sequence.  Fallback to
 	 stepping over the breakpoint in-line.  */
-      return -1;
+      return DISPLACED_STEP_PREPARE_STATUS_ERROR;
     }
 
   /* Save the information we need to fix things up if the step
@@ -1770,20 +1773,21 @@ displaced_step_prepare_throw (thread_info *tp)
 
   displaced_debug_printf ("displaced pc to %s", paddress (gdbarch, copy));
 
-  return 1;
+  return DISPLACED_STEP_PREPARE_STATUS_OK;
 }
 
 /* Wrapper for displaced_step_prepare_throw that disabled further
    attempts at displaced stepping if we get a memory error.  */
 
-static int
+static displaced_step_prepare_status
 displaced_step_prepare (thread_info *thread)
 {
-  int prepared = -1;
+  displaced_step_prepare_status status
+    = DISPLACED_STEP_PREPARE_STATUS_ERROR;
 
   try
     {
-      prepared = displaced_step_prepare_throw (thread);
+      status = displaced_step_prepare_throw (thread);
     }
   catch (const gdb_exception_error &ex)
     {
@@ -1810,7 +1814,7 @@ displaced_step_prepare (thread_info *thread)
       displaced_state->failed_before = 1;
     }
 
-  return prepared;
+  return status;
 }
 
 static void
@@ -1840,22 +1844,24 @@ displaced_step_restore (struct displaced_step_inferior_state *displaced,
 				    displaced->step_copy));
 }
 
-/* If we displaced stepped an instruction successfully, adjust
-   registers and memory to yield the same effect the instruction would
-   have had if we had executed it at its original address, and return
-   1.  If the instruction didn't complete, relocate the PC and return
-   -1.  If the thread wasn't displaced stepping, return 0.  */
+/* If we displaced stepped an instruction successfully, adjust registers and
+   memory to yield the same effect the instruction would have had if we had
+   executed it at its original address, and return
+   DISPLACED_STEP_PREPARE_STATUS_OK.  If the instruction didn't complete,
+   relocate the PC and return DISPLACED_STEP_PREPARE_STATUS_NOT_EXECUTED.
 
-static int
+   If the thread wasn't displaced stepping, return
+   DISPLACED_STEP_PREPARE_STATUS_OK as well..  */
+
+static displaced_step_finish_status
 displaced_step_finish (thread_info *event_thread, enum gdb_signal signal)
 {
   struct displaced_step_inferior_state *displaced
     = get_displaced_stepping_state (event_thread->inf);
-  int ret;
 
   /* Was this event for the thread we displaced?  */
   if (displaced->step_thread != event_thread)
-    return 0;
+    return DISPLACED_STEP_FINISH_STATUS_OK;
 
   /* Fixup may need to read memory/registers.  Switch to the thread
      that we're fixing up.  Also, target_stopped_by_watchpoint checks
@@ -1879,7 +1885,8 @@ displaced_step_finish (thread_info *event_thread, enum gdb_signal signal)
 				    displaced->step_original,
 				    displaced->step_copy,
 				    get_thread_regcache (displaced->step_thread));
-      ret = 1;
+
+      return DISPLACED_STEP_FINISH_STATUS_OK;
     }
   else
     {
@@ -1890,10 +1897,9 @@ displaced_step_finish (thread_info *event_thread, enum gdb_signal signal)
 
       pc = displaced->step_original + (pc - displaced->step_copy);
       regcache_write_pc (regcache, pc);
-      ret = -1;
-    }
 
-  return ret;
+      return DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED;
+    }
 }
 
 /* Data to be passed around while handling an event.  This data is
@@ -2417,16 +2423,17 @@ resume_1 (enum gdb_signal sig)
       && sig == GDB_SIGNAL_0
       && !current_inferior ()->waiting_for_vfork_done)
     {
-      int prepared = displaced_step_prepare (tp);
+      displaced_step_prepare_status prepare_status
+	= displaced_step_prepare (tp);
 
-      if (prepared == 0)
+      if (prepare_status == DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE)
 	{
 	  infrun_debug_printf ("Got placed in step-over queue");
 
 	  tp->control.trap_expected = 0;
 	  return;
 	}
-      else if (prepared < 0)
+      else if (prepare_status == DISPLACED_STEP_PREPARE_STATUS_ERROR)
 	{
 	  /* Fallback to stepping over the breakpoint in-line.  */
 
@@ -2440,7 +2447,7 @@ resume_1 (enum gdb_signal sig)
 
 	  insert_breakpoints ();
 	}
-      else if (prepared > 0)
+      else if (prepare_status == DISPLACED_STEP_PREPARE_STATUS_OK)
 	{
 	  /* Update pc to reflect the new address from which we will
 	     execute instructions due to displaced stepping.  */
@@ -2448,6 +2455,9 @@ resume_1 (enum gdb_signal sig)
 
 	  step = gdbarch_displaced_step_hw_singlestep (gdbarch);
 	}
+      else
+	gdb_assert_not_reached (_("Invalid displaced_step_prepare_status "
+				  "value."));
     }
 
   /* Do we need to do it the hard way, w/temp breakpoints?  */
@@ -4822,7 +4832,8 @@ stop_all_threads (void)
 		      t->suspend.waitstatus.kind = TARGET_WAITKIND_IGNORE;
 		      t->suspend.waitstatus_pending_p = 0;
 
-		      if (displaced_step_finish (t, GDB_SIGNAL_0) < 0)
+		      if (displaced_step_finish (t, GDB_SIGNAL_0)
+			  == DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED)
 			{
 			  /* Add it back to the step-over queue.  */
 			  infrun_debug_printf
@@ -4850,7 +4861,8 @@ stop_all_threads (void)
 		      sig = (event.ws.kind == TARGET_WAITKIND_STOPPED
 			     ? event.ws.value.sig : GDB_SIGNAL_0);
 
-		      if (displaced_step_finish (t, sig) < 0)
+		      if (displaced_step_finish (t, sig)
+			  == DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED)
 			{
 			  /* Add it back to the step-over queue.  */
 			  t->control.trap_expected = 0;
-- 
2.28.0


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

* [PATCH 07/12] gdb: pass inferior to get_linux_inferior_data
  2020-11-10 21:46 [PATCH 00/12] Concurrent displaced stepping Simon Marchi
                   ` (5 preceding siblings ...)
  2020-11-10 21:46 ` [PATCH 06/12] gdb: introduce status enum for displaced step prepare/finish Simon Marchi
@ 2020-11-10 21:46 ` Simon Marchi
  2020-11-25  1:30   ` Pedro Alves
  2020-11-10 21:46 ` [PATCH 08/12] gdb: move displaced stepping types to displaced-stepping.{h, c} Simon Marchi
                   ` (4 subsequent siblings)
  11 siblings, 1 reply; 44+ messages in thread
From: Simon Marchi @ 2020-11-10 21:46 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

Pass to get_linux_inferior_data the inferior for which we want to obtain
the linux-specific data, rather than assuming the current inferior.
This helps slightly reduce the diff in the upcoming main patch.

Update the sole caller to pass the current inferior.

gdb/ChangeLog:

	* linux-tdep.c (get_linux_inferior_data): Add inferior
	parameter.
	(linux_vsyscall_range): Pass current inferior.

Change-Id: Ie4b61190e4a2e89b5b55a140cfecd4de66d92393
---
 gdb/linux-tdep.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c
index bacb61398fa..c57181765e0 100644
--- a/gdb/linux-tdep.c
+++ b/gdb/linux-tdep.c
@@ -217,13 +217,11 @@ invalidate_linux_cache_inf (struct inferior *inf)
    valid INFO pointer.  */
 
 static struct linux_info *
-get_linux_inferior_data (void)
+get_linux_inferior_data (inferior *inf)
 {
-  struct linux_info *info;
-  struct inferior *inf = current_inferior ();
+  linux_info *info = linux_inferior_data.get (inf);
 
-  info = linux_inferior_data.get (inf);
-  if (info == NULL)
+  if (info == nullptr)
     info = linux_inferior_data.emplace (inf);
 
   return info;
@@ -2408,7 +2406,7 @@ linux_vsyscall_range_raw (struct gdbarch *gdbarch, struct mem_range *range)
 static int
 linux_vsyscall_range (struct gdbarch *gdbarch, struct mem_range *range)
 {
-  struct linux_info *info = get_linux_inferior_data ();
+  struct linux_info *info = get_linux_inferior_data (current_inferior ());
 
   if (info->vsyscall_range_p == 0)
     {
-- 
2.28.0


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

* [PATCH 08/12] gdb: move displaced stepping types to displaced-stepping.{h, c}
  2020-11-10 21:46 [PATCH 00/12] Concurrent displaced stepping Simon Marchi
                   ` (6 preceding siblings ...)
  2020-11-10 21:46 ` [PATCH 07/12] gdb: pass inferior to get_linux_inferior_data Simon Marchi
@ 2020-11-10 21:46 ` Simon Marchi
  2020-11-25  1:30   ` Pedro Alves
  2020-11-10 21:46 ` [PATCH 09/12] gdb: move displaced stepping logic to gdbarch, allow starting concurrent displaced steps Simon Marchi
                   ` (3 subsequent siblings)
  11 siblings, 1 reply; 44+ messages in thread
From: Simon Marchi @ 2020-11-10 21:46 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

Move displaced-stepping related stuff unchanged to displaced-stepping.h
and displaced-stepping.c.  This helps make the following patch a bit
smaller and easier to read.

gdb/ChangeLog:

	* Makefile.in (COMMON_SFILES): Add displaced-stepping.c.
	* aarch64-tdep.h: Include displaced-stepping.h.
	* displaced-stepping.h (struct displaced_step_copy_insn_closure):
	Move here.
	(displaced_step_copy_insn_closure_up): Move here.
	(struct buf_displaced_step_copy_insn_closure): Move here.
	(struct displaced_step_inferior_state): Move here.
	(debug_displaced): Move here.
	(displaced_debug_printf_1): Move here.
	(displaced_debug_printf): Move here.
	* displaced-stepping.c: New file.
	* gdbarch.sh: Include displaced-stepping.h in gdbarch.h.
	* gdbarch.h: Re-generate.
	* inferior.h: Include displaced-stepping.h.
	* infrun.h (debug_displaced): Move to displaced-stepping.h.
	(displaced_debug_printf_1): Likewise.
	(displaced_debug_printf): Likewise.
	(struct displaced_step_copy_insn_closure): Likewise.
	(displaced_step_copy_insn_closure_up): Likewise.
	(struct buf_displaced_step_copy_insn_closure): Likewise.
	(struct displaced_step_inferior_state): Likewise.
	* infrun.c (show_debug_displaced): Move to displaced-stepping.c.
	(displaced_debug_printf_1): Likewise.
	(displaced_step_copy_insn_closure::~displaced_step_copy_insn_closure):
	Likewise.
	(_initialize_infrun): Don't register "set/show debug displaced".

Change-Id: I29935f5959b80425370630a45148fc06cd4227ca
---
 gdb/Makefile.in          |  1 +
 gdb/aarch64-tdep.h       |  1 +
 gdb/displaced-stepping.c | 52 +++++++++++++++++++++++++
 gdb/displaced-stepping.h | 84 ++++++++++++++++++++++++++++++++++++++++
 gdb/gdbarch.h            |  1 +
 gdb/gdbarch.sh           |  1 +
 gdb/inferior.h           |  1 +
 gdb/infrun.c             | 22 -----------
 gdb/infrun.h             | 78 -------------------------------------
 9 files changed, 141 insertions(+), 100 deletions(-)
 create mode 100644 gdb/displaced-stepping.c

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index c46935efafa..f8a962549ac 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -1015,6 +1015,7 @@ COMMON_SFILES = \
 	debuginfod-support.c \
 	dictionary.c \
 	disasm.c \
+	displaced-stepping.c \
 	dummy-frame.c \
 	dwarf2/abbrev.c \
 	dwarf2/attribute.c \
diff --git a/gdb/aarch64-tdep.h b/gdb/aarch64-tdep.h
index 895aa5977f0..76ff812abc6 100644
--- a/gdb/aarch64-tdep.h
+++ b/gdb/aarch64-tdep.h
@@ -23,6 +23,7 @@
 #define AARCH64_TDEP_H
 
 #include "arch/aarch64.h"
+#include "displaced-stepping.h"
 #include "infrun.h"
 
 /* Forward declarations.  */
diff --git a/gdb/displaced-stepping.c b/gdb/displaced-stepping.c
new file mode 100644
index 00000000000..5ae280fac39
--- /dev/null
+++ b/gdb/displaced-stepping.c
@@ -0,0 +1,52 @@
+/* Displaced stepping related things.
+
+   Copyright (C) 2020 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include "defs.h"
+#include "displaced-stepping.h"
+#include "cli/cli-cmds.h"
+
+#include "command.h"
+
+/* Default destructor for displaced_step_copy_insn_closure.  */
+
+displaced_step_copy_insn_closure::~displaced_step_copy_insn_closure ()
+  = default;
+
+bool debug_displaced = false;
+
+static void
+show_debug_displaced (struct ui_file *file, int from_tty,
+		      struct cmd_list_element *c, const char *value)
+{
+  fprintf_filtered (file, _("Displace stepping debugging is %s.\n"), value);
+}
+
+void _initialize_displaced_stepping ();
+void
+_initialize_displaced_stepping ()
+{
+  add_setshow_boolean_cmd ("displaced", class_maintenance,
+			   &debug_displaced, _("\
+Set displaced stepping debugging."), _("\
+Show displaced stepping debugging."), _("\
+When non-zero, displaced stepping specific debugging is enabled."),
+			    NULL,
+			    show_debug_displaced,
+			    &setdebuglist, &showdebuglist);
+}
diff --git a/gdb/displaced-stepping.h b/gdb/displaced-stepping.h
index 9c7e85c3769..99343f84e44 100644
--- a/gdb/displaced-stepping.h
+++ b/gdb/displaced-stepping.h
@@ -20,6 +20,24 @@
 #ifndef DISPLACED_STEPPING_H
 #define DISPLACED_STEPPING_H
 
+#include "gdbsupport/byte-vector.h"
+
+struct thread_info;
+
+/* True if we are debugging displaced stepping.  */
+
+extern bool debug_displaced;
+
+/* Print a "displaced" debug statement.  */
+
+#define displaced_debug_printf(fmt, ...) \
+  do \
+    { \
+      if (debug_displaced) \
+	debug_prefixed_printf ("displaced", __func__, fmt, ##__VA_ARGS__); \
+    } \
+  while (0)
+
 enum displaced_step_prepare_status
 {
   /* A displaced stepping buffer was successfully allocated and prepared.  */
@@ -43,4 +61,70 @@ enum displaced_step_finish_status
   DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED,
 };
 
+/* Base class for displaced stepping closures (the arch-specific data).  */
+
+struct displaced_step_copy_insn_closure
+{
+  virtual ~displaced_step_copy_insn_closure () = 0;
+};
+
+using displaced_step_copy_insn_closure_up
+  = std::unique_ptr<displaced_step_copy_insn_closure>;
+
+/* A simple displaced step closure that contains only a byte buffer.  */
+
+struct buf_displaced_step_copy_insn_closure : displaced_step_copy_insn_closure
+{
+  buf_displaced_step_copy_insn_closure (int buf_size)
+  : buf (buf_size)
+  {}
+
+  gdb::byte_vector buf;
+};
+
+/* Per-inferior displaced stepping state.  */
+
+struct displaced_step_inferior_state
+{
+  displaced_step_inferior_state ()
+  {
+    reset ();
+  }
+
+  /* Put this object back in its original state.  */
+  void reset ()
+  {
+    failed_before = 0;
+    step_thread = nullptr;
+    step_gdbarch = nullptr;
+    step_closure.reset ();
+    step_original = 0;
+    step_copy = 0;
+    step_saved_copy.clear ();
+  }
+
+  /* True if preparing a displaced step ever failed.  If so, we won't
+     try displaced stepping for this inferior again.  */
+  int failed_before;
+
+  /* If this is not nullptr, this is the thread carrying out a
+     displaced single-step in process PID.  This thread's state will
+     require fixing up once it has completed its step.  */
+  thread_info *step_thread;
+
+  /* The architecture the thread had when we stepped it.  */
+  gdbarch *step_gdbarch;
+
+  /* The closure provided gdbarch_displaced_step_copy_insn, to be used
+     for post-step cleanup.  */
+  displaced_step_copy_insn_closure_up step_closure;
+
+  /* The address of the original instruction, and the copy we
+     made.  */
+  CORE_ADDR step_original, step_copy;
+
+  /* Saved contents of copy area.  */
+  gdb::byte_vector step_saved_copy;
+};
+
 #endif /* DISPLACED_STEPPING_H */
diff --git a/gdb/gdbarch.h b/gdb/gdbarch.h
index 0e4e4da0c1b..883f8fb3329 100644
--- a/gdb/gdbarch.h
+++ b/gdb/gdbarch.h
@@ -31,6 +31,7 @@
 #include "gdb_obstack.h"
 #include "infrun.h"
 #include "osabi.h"
+#include "displaced-stepping.h"
 
 struct floatformat;
 struct ui_file;
diff --git a/gdb/gdbarch.sh b/gdb/gdbarch.sh
index d096b7bffec..47007807c1e 100755
--- a/gdb/gdbarch.sh
+++ b/gdb/gdbarch.sh
@@ -1270,6 +1270,7 @@ cat <<EOF
 #include "gdb_obstack.h"
 #include "infrun.h"
 #include "osabi.h"
+#include "displaced-stepping.h"
 
 struct floatformat;
 struct ui_file;
diff --git a/gdb/inferior.h b/gdb/inferior.h
index d016161fb05..c5257ac1e64 100644
--- a/gdb/inferior.h
+++ b/gdb/inferior.h
@@ -60,6 +60,7 @@ struct thread_info;
 #include "gdbthread.h"
 
 #include "process-stratum-target.h"
+#include "displaced-stepping.h"
 
 struct infcall_suspend_state;
 struct infcall_control_state;
diff --git a/gdb/infrun.c b/gdb/infrun.c
index ed54c4331d7..d09209cd8d4 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -153,14 +153,6 @@ static ptid_t previous_inferior_ptid;
 
 static bool detach_fork = true;
 
-bool debug_displaced = false;
-static void
-show_debug_displaced (struct ui_file *file, int from_tty,
-		      struct cmd_list_element *c, const char *value)
-{
-  fprintf_filtered (file, _("Displace stepping debugging is %s.\n"), value);
-}
-
 unsigned int debug_infrun = 0;
 static void
 show_debug_infrun (struct ui_file *file, int from_tty,
@@ -1460,11 +1452,6 @@ step_over_info_valid_p (void)
    displaced step operation on it.  See displaced_step_prepare and
    displaced_step_finish for details.  */
 
-/* Default destructor for displaced_step_copy_insn_closure.  */
-
-displaced_step_copy_insn_closure::~displaced_step_copy_insn_closure ()
-  = default;
-
 /* Get the displaced stepping state of inferior INF.  */
 
 static displaced_step_inferior_state *
@@ -9302,15 +9289,6 @@ When non-zero, inferior specific debugging is enabled."),
 			     show_debug_infrun,
 			     &setdebuglist, &showdebuglist);
 
-  add_setshow_boolean_cmd ("displaced", class_maintenance,
-			   &debug_displaced, _("\
-Set displaced stepping debugging."), _("\
-Show displaced stepping debugging."), _("\
-When non-zero, displaced stepping specific debugging is enabled."),
-			    NULL,
-			    show_debug_displaced,
-			    &setdebuglist, &showdebuglist);
-
   add_setshow_boolean_cmd ("non-stop", no_class,
 			   &non_stop_1, _("\
 Set whether gdb controls the inferior in non-stop mode."), _("\
diff --git a/gdb/infrun.h b/gdb/infrun.h
index a276ddf0250..c83cb333083 100644
--- a/gdb/infrun.h
+++ b/gdb/infrun.h
@@ -41,19 +41,6 @@ extern unsigned int debug_infrun;
     } \
   while (0)
 
-/* True if we are debugging displaced stepping.  */
-extern bool debug_displaced;
-
-/* Print a "displaced" debug statement.  */
-
-#define displaced_debug_printf(fmt, ...) \
-  do \
-    { \
-      if (debug_displaced) \
-	debug_prefixed_printf ("displaced", __func__, fmt, ##__VA_ARGS__); \
-    } \
-  while (0)
-
 /* Nonzero if we want to give control to the user when we're notified
    of shared library events by the dynamic linker.  */
 extern int stop_on_solib_events;
@@ -280,69 +267,4 @@ extern void all_uis_check_sync_execution_done (void);
    started or re-started).  */
 extern void all_uis_on_sync_execution_starting (void);
 
-/* Base class for displaced stepping closures (the arch-specific data).  */
-
-struct displaced_step_copy_insn_closure
-{
-  virtual ~displaced_step_copy_insn_closure () = 0;
-};
-
-using displaced_step_copy_insn_closure_up
-  = std::unique_ptr<displaced_step_copy_insn_closure>;
-
-/* A simple displaced step closure that contains only a byte buffer.  */
-
-struct buf_displaced_step_copy_insn_closure : displaced_step_copy_insn_closure
-{
-  buf_displaced_step_copy_insn_closure (int buf_size)
-  : buf (buf_size)
-  {}
-
-  gdb::byte_vector buf;
-};
-
-/* Per-inferior displaced stepping state.  */
-struct displaced_step_inferior_state
-{
-  displaced_step_inferior_state ()
-  {
-    reset ();
-  }
-
-  /* Put this object back in its original state.  */
-  void reset ()
-  {
-    failed_before = 0;
-    step_thread = nullptr;
-    step_gdbarch = nullptr;
-    step_closure.reset ();
-    step_original = 0;
-    step_copy = 0;
-    step_saved_copy.clear ();
-  }
-
-  /* True if preparing a displaced step ever failed.  If so, we won't
-     try displaced stepping for this inferior again.  */
-  int failed_before;
-
-  /* If this is not nullptr, this is the thread carrying out a
-     displaced single-step in process PID.  This thread's state will
-     require fixing up once it has completed its step.  */
-  thread_info *step_thread;
-
-  /* The architecture the thread had when we stepped it.  */
-  gdbarch *step_gdbarch;
-
-  /* The closure provided gdbarch_displaced_step_copy_insn, to be used
-     for post-step cleanup.  */
-  displaced_step_copy_insn_closure_up step_closure;
-
-  /* The address of the original instruction, and the copy we
-     made.  */
-  CORE_ADDR step_original, step_copy;
-
-  /* Saved contents of copy area.  */
-  gdb::byte_vector step_saved_copy;
-};
-
 #endif /* INFRUN_H */
-- 
2.28.0


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

* [PATCH 09/12] gdb: move displaced stepping logic to gdbarch, allow starting concurrent displaced steps
  2020-11-10 21:46 [PATCH 00/12] Concurrent displaced stepping Simon Marchi
                   ` (7 preceding siblings ...)
  2020-11-10 21:46 ` [PATCH 08/12] gdb: move displaced stepping types to displaced-stepping.{h, c} Simon Marchi
@ 2020-11-10 21:46 ` Simon Marchi
  2020-11-25  1:40   ` Pedro Alves
  2020-11-10 21:46 ` [PATCH 10/12] gdb: change linux gdbarch data from post to pre-init Simon Marchi
                   ` (2 subsequent siblings)
  11 siblings, 1 reply; 44+ messages in thread
From: Simon Marchi @ 2020-11-10 21:46 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

Today, GDB only allows a single displaced stepping operation to happen
per inferior at a time.  There is a single displaced stepping buffer per
inferior, whose address is fixed (obtained with
gdbarch_displaced_step_location), managed by infrun.c.

In the case of the AMD ROCm target [1] (in the context of which this
work has been done), it is typical to have thousands of threads (or
waves, in SMT terminology) executing the same code, hitting the same
breakpoint (possibly conditional) and needing to to displaced step it at
the same time.  The limitation of only one displaced step executing at a
any given time becomes a real bottleneck.

To fix this bottleneck, we want to make it possible for threads of a
same inferior to execute multiple displaced steps in parallel.  This
patch builds the foundation for that.

In essence, this patch moves the task of preparing a displaced step and
cleaning up after to gdbarch functions.  This allows using different
schemes for allocating and managing displaced stepping buffers for
different platforms.  The gdbarch decides how to assign a buffer to a
thread that needs to execute a displaced step.

On the ROCm target, we are able to allocate one displaced stepping
buffer per thread, so a thread will never have to wait to execute a
displaced step.

On Linux, the entry point of the executable if used as the displaced
stepping buffer, since we assume that this code won't get used after
startup.  From what I saw (I checked with a binary generated against
glibc and musl), on AMD64 we have enough space there to fit two
displaced stepping buffers.  A subsequent patch makes AMD64/Linux use
two buffers.

In addition to having multiple displaced stepping buffers, there is also
the idea of sharing displaced stepping buffers between threads.  Two
threads doing displaced steps for the same PC could use the same buffer
at the same time.  Two threads stepping over the same instruction (same
opcode) at two different PCs may also be able to share a displaced
stepping buffer.  This is an idea for future patches, but the
architecture built by this patch is made to allow this.

Now, the implementation details.  The main part of this patch is moving
the responsibility of preparing and finishing a displaced step to the
gdbarch.  Before this patch, preparing a displaced step is driven by the
displaced_step_prepare_throw function.  It does some calls to the
gdbarch to do some low-level operations, but the high-level logic is
there.  The steps are roughly:

- Ask the gdbarch for the displaced step buffer location
- Save the existing bytes in the displaced step buffer
- Ask the gdbarch to copy the instruction into the displaced step buffer
- Set the pc of the thread to the beginning of the displaced step buffer

Similarly, the "fixup" phase, executed after the instruction was
successfully single-stepped, is driven by the infrun code (function
displaced_step_fixup).  The steps are roughly:

- Restore the original bytes in the displaced stepping buffer
- Ask the gdbarch to fixup the instruction result (adjust the target's
  registers or memory to do as if the instruction had been executed in
  its original location)

The displaced_step_inferior_state::step_thread field indicates which
thread (if any) is currently using the displaced stepping buffer, so it
is used by displaced_step_prepare_throw to check if the displaced
stepping buffer is free to use or not.

This patch defers the whole task of preparing and cleaning up after a
displaced step to the gdbarch.  Two new main gdbarch methods are added,
with the following sematics:

  - gdbarch_displaced_step_prepare: Prepare for the given thread to
    execute a displaced step of the instruction located at its current PC.
    Upon return, everything should be ready for GDB to resume the thread
    (with either a single step or continue, as indicated by
    gdbarch_displaced_step_hw_singlestep) to make it displaced step the
    instruction.

  - gdbarch_displaced_step_finish: Called when the thread stopped after
    having started a displaced step.  Verify if the instruction was
    executed, if so apply any fixup required to compensate for the fact
    that the instruction was executed at different place than its original
    pc.  Release any resources that was allocated for this displaced step.
    Upon return, everything should be ready for GDB to resume the
    thread in its "normal" code path.

The displaced_step_prepare_throw function now pretty much just offloads
to gdbarch_displaced_step_prepare and the displaced_step_finish function
offloads to gdbarch_displaced_step_finish.

The gdbarch_displaced_step_location method is now unnecessary, so is
removed.  Indeed, the core of GDB doesn't know how many displaced step
buffers there are nor where they are.

To keep the existing behavior for existing architectures, the logic that
was previously implemented in infrun.c for preparing and finishing a
displaced step is moved to displaced-stepping.c, to the
displaced_step_buffer class.  Architectures are modified to implement
the new gdbarch methods using this class.  The behavior is not expeicted
to change.

The other important change (which arises from the above) is that the
core of GDB no longer prevents concurrent displaced steps.  Before this
patch, start_step_over walks the global step over chain and tries to
initiate a step over (whether it is in-line or displaced).  It follows
these rules:

  - if an in-line step is in progress (in any inferior), don't start any
    other step over
  - if a displaced step is in progress for an inferior, don't start
    another displaced step for that inferior

After starting a displaced step for a given inferior, it won't start
another displaced step for that inferior.

In the new code, start_step_over simply tries to initiate step overs for
all the threads in the list.  But because threads may be added back to
the global list as it iterates the global list, trying to initiate step
overs, start_step_over now starts by stealing the global queue into a
local queue and iterates on the local queue.  In the typical case, each
thread will either:

  - have initiated a displaced step and be resumed
  - have been added back by the global step over queue by
    displaced_step_prepare_throw, because the gdbarch will have returned
    that there aren't enough resources (i.e. buffers) to initiate a
    displaced step for that thread

Lastly, if start_step_over initiates an in-line step, it stops
iterating, and moves back whatever remaining threads it had in its local
step over queue to the global step over queue.

Two other gdbarch methods are added, to handle some slightly annoying
corner cases.  They feel awkwardly specific to these cases, but I don't
see any way around them:

  - gdbarch_displaced_step_copy_insn_closure_by_addr: in
    arm_pc_is_thumb, arm-tdep.c wants to get the closure for a given
    buffer address.

  - gdbarch_displaced_step_restore_all_in_ptid: when a process forks
    (at least on Linux), the address space is copied.  If some displaced
    step buffers were in use at the time of the fork, we need to restore
    the original bytes in the child's address space.

These two adjustments are also made in infrun.c:

  - prepare_for_detach: there may be multiple threads doing displaced
    steps when we detach, so wait until all of them are done

  - handle_inferior_event: when we handle a fork event for a given
    thread, it's possible that other threads are doing a displaced step at
    the same time.  Make sure to restore the displaced step buffer
    contents in the child for them.

[1] https://github.com/ROCm-Developer-Tools/ROCgdb

gdb/ChangeLog:

	* displaced-stepping.h (struct
	displaced_step_copy_insn_closure): Adjust comments.
	(struct displaced_step_inferior_state) <step_thread,
	step_gdbarch, step_closure, step_original, step_copy,
	step_saved_copy>: Remove fields.
	(struct displaced_step_thread_state): New.
	(struct displaced_step_buffer): New.
	* displaced-stepping.c (displaced_step_buffer::prepare): New.
	(write_memory_ptid): Move from infrun.c.
	(displaced_step_instruction_executed_successfully): New,
	factored out of displaced_step_finish.
	(displaced_step_buffer::finish): New.
	(displaced_step_buffer::copy_insn_closure_by_addr): New.
	(displaced_step_buffer::restore_in_ptid): New.
	* gdbarch.sh (displaced_step_location): Remove.
	(displaced_step_prepare, displaced_step_finish,
	displaced_step_copy_insn_closure_by_addr,
	displaced_step_restore_all_in_ptid): New.
	* gdbarch.c: Re-generate.
	* gdbarch.h: Re-generate.
	* gdbthread.h (class thread_info) <displaced_step_state>: New
	field.
	(thread_step_over_chain_remove): New declaration.
	(thread_step_over_chain_next): New declaration.
	(thread_step_over_chain_length): New declaration.
	* thread.c (thread_step_over_chain_remove): Make non-static.
	(thread_step_over_chain_next): New.
	(global_thread_step_over_chain_next): Use
	thread_step_over_chain_next.
	(thread_step_over_chain_length): New.
	(global_thread_step_over_chain_enqueue): Add debug print.
	(global_thread_step_over_chain_remove): Add debug print.
	* infrun.h (get_displaced_step_copy_insn_closure_by_addr):
	Remove.
	* infrun.c (get_displaced_stepping_state): New.
	(displaced_step_in_progress_any_inferior): Remove.
	(displaced_step_in_progress_thread): Adjust.
	(displaced_step_in_progress): Adjust.
	(displaced_step_in_progress_any_thread): New.
	(get_displaced_step_copy_insn_closure_by_addr): Remove.
	(gdbarch_supports_displaced_stepping): Use
	gdbarch_displaced_step_prepare_p.
	(displaced_step_reset): Change parameter from inferior to
	thread.
	(displaced_step_prepare_throw): Implement using
	gdbarch_displaced_step_prepare.
	(write_memory_ptid): Move to displaced-step.c.
	(displaced_step_restore): Remove.
	(displaced_step_finish): Implement using
	gdbarch_displaced_step_finish.
	(start_step_over): Allow starting more than one displaced step.
	(prepare_for_detach): Handle possibly multiple threads doing
	displaced steps.
	(handle_inferior_event): Handle possibility that fork event
	happens while another thread displaced steps.
	* linux-tdep.h (linux_displaced_step_prepare): New.
	(linux_displaced_step_finish): New.
	(linux_displaced_step_copy_insn_closure_by_addr): New.
	(linux_displaced_step_restore_all_in_ptid): New.
	(linux_init_abi): Add supports_displaced_step parameter.
	* linux-tdep.c (struct linux_info) <disp_step_buf>: New field.
	(linux_displaced_step_prepare): New.
	(linux_displaced_step_finish): New.
	(linux_displaced_step_copy_insn_closure_by_addr): New.
	(linux_displaced_step_restore_all_in_ptid): New.
	(linux_init_abi): Add supports_displaced_step parameter,
	register displaced step methods if true.
	(_initialize_linux_tdep): Register inferior_execd observer.
	* amd64-linux-tdep.c (amd64_linux_init_abi_common): Add
	supports_displaced_step parameter, adjust call to
	linux_init_abi.  Remove call to
	set_gdbarch_displaced_step_location.
	(amd64_linux_init_abi): Adjust call to
	amd64_linux_init_abi_common.
	(amd64_x32_linux_init_abi): Likewise.
	* aarch64-linux-tdep.c (aarch64_linux_init_abi): Adjust call to
	linux_init_abi.  Remove call to
	set_gdbarch_displaced_step_location.
	* arm-linux-tdep.c (arm_linux_init_abi): Likewise.
	* i386-linux-tdep.c (i386_linux_init_abi): Likewise.
	* alpha-linux-tdep.c (alpha_linux_init_abi): Adjust call to
	linux_init_abi.
	* arc-linux-tdep.c (arc_linux_init_osabi): Likewise.
	* bfin-linux-tdep.c (bfin_linux_init_abi): Likewise.
	* cris-linux-tdep.c (cris_linux_init_abi): Likewise.
	* csky-linux-tdep.c (csky_linux_init_abi): Likewise.
	* frv-linux-tdep.c (frv_linux_init_abi): Likewise.
	* hppa-linux-tdep.c (hppa_linux_init_abi): Likewise.
	* ia64-linux-tdep.c (ia64_linux_init_abi): Likewise.
	* m32r-linux-tdep.c (m32r_linux_init_abi): Likewise.
	* m68k-linux-tdep.c (m68k_linux_init_abi): Likewise.
	* microblaze-linux-tdep.c (microblaze_linux_init_abi): Likewise.
	* mips-linux-tdep.c (mips_linux_init_abi): Likewise.
	* mn10300-linux-tdep.c (am33_linux_init_osabi): Likewise.
	* nios2-linux-tdep.c (nios2_linux_init_abi): Likewise.
	* or1k-linux-tdep.c (or1k_linux_init_abi): Likewise.
	* riscv-linux-tdep.c (riscv_linux_init_abi): Likewise.
	* s390-linux-tdep.c (s390_linux_init_abi_any): Likewise.
	* sh-linux-tdep.c (sh_linux_init_abi): Likewise.
	* sparc-linux-tdep.c (sparc32_linux_init_abi): Likewise.
	* sparc64-linux-tdep.c (sparc64_linux_init_abi): Likewise.
	* tic6x-linux-tdep.c (tic6x_uclinux_init_abi): Likewise.
	* tilegx-linux-tdep.c (tilegx_linux_init_abi): Likewise.
	* xtensa-linux-tdep.c (xtensa_linux_init_abi): Likewise.
	* ppc-linux-tdep.c (ppc_linux_init_abi): Adjust call to
	linux_init_abi.  Remove call to
	set_gdbarch_displaced_step_location.
	* arm-tdep.c (arm_pc_is_thumb): Call
	gdbarch_displaced_step_copy_insn_closure_by_addr instead of
	get_displaced_step_copy_insn_closure_by_addr.
	* rs6000-aix-tdep.c (rs6000_aix_init_osabi): Adjust calls to
	clear gdbarch methods.
	* rs6000-tdep.c (struct ppc_inferior_data): New structure.
	(get_ppc_per_inferior): New function.
	(ppc_displaced_step_prepare): New function.
	(ppc_displaced_step_finish): New function.
	(ppc_displaced_step_restore_all_in_ptid): New function.
	(rs6000_gdbarch_init): Register new gdbarch methods.
	* s390-tdep.c (s390_gdbarch_init): Don't call
	set_gdbarch_displaced_step_location, set new gdbarch methods.

gdb/testsuite/ChangeLog:

	* gdb.arch/amd64-disp-step-avx.exp: Adjust pattern.
	* gdb.threads/forking-threads-plus-breakpoint.exp: Likewise.
	* gdb.threads/non-stop-fair-events.exp: Likewise.

Change-Id: I387cd235a442d0620ec43608fd3dc0097fcbf8c8
---
 gdb/aarch64-linux-tdep.c                      |   3 +-
 gdb/alpha-linux-tdep.c                        |   2 +-
 gdb/amd64-linux-tdep.c                        |  11 +-
 gdb/arc-linux-tdep.c                          |   2 +-
 gdb/arm-linux-tdep.c                          |   3 +-
 gdb/arm-tdep.c                                |   9 +-
 gdb/bfin-linux-tdep.c                         |   2 +-
 gdb/cris-linux-tdep.c                         |   2 +-
 gdb/csky-linux-tdep.c                         |   2 +-
 gdb/displaced-stepping.c                      | 190 +++++++++-
 gdb/displaced-stepping.h                      |  92 +++--
 gdb/frv-linux-tdep.c                          |   2 +-
 gdb/gdbarch.c                                 | 113 +++++-
 gdb/gdbarch.h                                 |  38 +-
 gdb/gdbarch.sh                                |  21 +-
 gdb/gdbthread.h                               |  19 +
 gdb/hppa-linux-tdep.c                         |   2 +-
 gdb/i386-linux-tdep.c                         |   4 +-
 gdb/ia64-linux-tdep.c                         |   2 +-
 gdb/infrun.c                                  | 331 +++++++-----------
 gdb/infrun.h                                  |   3 -
 gdb/linux-tdep.c                              |  72 +++-
 gdb/linux-tdep.h                              |  27 +-
 gdb/m32r-linux-tdep.c                         |   2 +-
 gdb/m68k-linux-tdep.c                         |   2 +-
 gdb/microblaze-linux-tdep.c                   |   2 +-
 gdb/mips-linux-tdep.c                         |   2 +-
 gdb/mn10300-linux-tdep.c                      |   2 +-
 gdb/nios2-linux-tdep.c                        |   2 +-
 gdb/or1k-linux-tdep.c                         |   2 +-
 gdb/ppc-linux-tdep.c                          |   5 +-
 gdb/riscv-linux-tdep.c                        |   2 +-
 gdb/rs6000-aix-tdep.c                         |   6 +-
 gdb/rs6000-tdep.c                             |  78 ++++-
 gdb/s390-linux-tdep.c                         |   2 +-
 gdb/s390-tdep.c                               |   5 +-
 gdb/sh-linux-tdep.c                           |   2 +-
 gdb/sparc-linux-tdep.c                        |   2 +-
 gdb/sparc64-linux-tdep.c                      |   2 +-
 .../gdb.arch/amd64-disp-step-avx.exp          |   2 +-
 .../forking-threads-plus-breakpoint.exp       |   2 +-
 .../gdb.threads/non-stop-fair-events.exp      |   2 +-
 gdb/thread.c                                  |  46 ++-
 gdb/tic6x-linux-tdep.c                        |   2 +-
 gdb/tilegx-linux-tdep.c                       |   2 +-
 gdb/xtensa-linux-tdep.c                       |   2 +-
 46 files changed, 810 insertions(+), 318 deletions(-)

diff --git a/gdb/aarch64-linux-tdep.c b/gdb/aarch64-linux-tdep.c
index c9898bdafda..4fe7babe59c 100644
--- a/gdb/aarch64-linux-tdep.c
+++ b/gdb/aarch64-linux-tdep.c
@@ -1445,7 +1445,7 @@ aarch64_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 
   tdep->lowest_pc = 0x8000;
 
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, true);
 
   set_solib_svr4_fetch_link_map_offsets (gdbarch,
 					 svr4_lp64_fetch_link_map_offsets);
@@ -1658,7 +1658,6 @@ aarch64_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
   set_gdbarch_displaced_step_copy_insn (gdbarch,
 					aarch64_displaced_step_copy_insn);
   set_gdbarch_displaced_step_fixup (gdbarch, aarch64_displaced_step_fixup);
-  set_gdbarch_displaced_step_location (gdbarch, linux_displaced_step_location);
   set_gdbarch_displaced_step_hw_singlestep (gdbarch,
 					    aarch64_displaced_step_hw_singlestep);
 
diff --git a/gdb/alpha-linux-tdep.c b/gdb/alpha-linux-tdep.c
index 70ac5a87766..a6d6b15e9fd 100644
--- a/gdb/alpha-linux-tdep.c
+++ b/gdb/alpha-linux-tdep.c
@@ -356,7 +356,7 @@ alpha_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
   struct gdbarch_tdep *tdep;
 
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   /* Hook into the DWARF CFI frame unwinder.  */
   alpha_dwarf2_init_abi (info, gdbarch);
diff --git a/gdb/amd64-linux-tdep.c b/gdb/amd64-linux-tdep.c
index d484b1a1c59..a81bb9039df 100644
--- a/gdb/amd64-linux-tdep.c
+++ b/gdb/amd64-linux-tdep.c
@@ -1795,11 +1795,12 @@ amd64_dtrace_parse_probe_argument (struct gdbarch *gdbarch,
 }
 
 static void
-amd64_linux_init_abi_common(struct gdbarch_info info, struct gdbarch *gdbarch)
+amd64_linux_init_abi_common(struct gdbarch_info info, struct gdbarch *gdbarch,
+			    bool supports_displaced_step)
 {
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
 
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, supports_displaced_step);
 
   tdep->sigtramp_p = amd64_linux_sigtramp_p;
   tdep->sigcontext_addr = amd64_linux_sigcontext_addr;
@@ -1839,8 +1840,6 @@ amd64_linux_init_abi_common(struct gdbarch_info info, struct gdbarch *gdbarch)
   set_gdbarch_displaced_step_copy_insn (gdbarch,
 					amd64_displaced_step_copy_insn);
   set_gdbarch_displaced_step_fixup (gdbarch, amd64_displaced_step_fixup);
-  set_gdbarch_displaced_step_location (gdbarch,
-				       linux_displaced_step_location);
 
   set_gdbarch_process_record (gdbarch, i386_process_record);
   set_gdbarch_process_record_signal (gdbarch, amd64_linux_record_signal);
@@ -1881,7 +1880,7 @@ amd64_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
   if (!valid_p)
     return;
 
-  amd64_linux_init_abi_common (info, gdbarch);
+  amd64_linux_init_abi_common (info, gdbarch, true);
 
   /* Initialize the amd64_linux_record_tdep.  */
   /* These values are the size of the type that will be used in a system
@@ -2096,7 +2095,7 @@ amd64_x32_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
   if (!valid_p)
     return;
 
-  amd64_linux_init_abi_common (info, gdbarch);
+  amd64_linux_init_abi_common (info, gdbarch, false);
 
   /* Initialize the amd64_x32_linux_record_tdep.  */
   /* These values are the size of the type that will be used in a system
diff --git a/gdb/arc-linux-tdep.c b/gdb/arc-linux-tdep.c
index 9ff5f1214a1..71dcdcc1811 100644
--- a/gdb/arc-linux-tdep.c
+++ b/gdb/arc-linux-tdep.c
@@ -434,7 +434,7 @@ arc_linux_init_osabi (struct gdbarch_info info, struct gdbarch *gdbarch)
    */
   tdep->jb_pc = 15;
 
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   /* Set up target dependent GDB architecture entries.  */
   set_gdbarch_cannot_fetch_register (gdbarch, arc_linux_cannot_fetch_register);
diff --git a/gdb/arm-linux-tdep.c b/gdb/arm-linux-tdep.c
index 9caae06adfe..d164cff3dff 100644
--- a/gdb/arm-linux-tdep.c
+++ b/gdb/arm-linux-tdep.c
@@ -1721,7 +1721,7 @@ arm_linux_init_abi (struct gdbarch_info info,
 								    NULL };
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
 
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, true);
 
   tdep->lowest_pc = 0x8000;
   if (info.byte_order_for_code == BFD_ENDIAN_BIG)
@@ -1807,7 +1807,6 @@ arm_linux_init_abi (struct gdbarch_info info,
   set_gdbarch_displaced_step_copy_insn (gdbarch,
 					arm_linux_displaced_step_copy_insn);
   set_gdbarch_displaced_step_fixup (gdbarch, arm_displaced_step_fixup);
-  set_gdbarch_displaced_step_location (gdbarch, linux_displaced_step_location);
 
   /* Reversible debugging, process record.  */
   set_gdbarch_process_record (gdbarch, arm_process_record);
diff --git a/gdb/arm-tdep.c b/gdb/arm-tdep.c
index e216ecf6fe5..ae09bbb9dc7 100644
--- a/gdb/arm-tdep.c
+++ b/gdb/arm-tdep.c
@@ -410,9 +410,12 @@ arm_pc_is_thumb (struct gdbarch *gdbarch, CORE_ADDR memaddr)
 {
   struct bound_minimal_symbol sym;
   char type;
-  arm_displaced_step_copy_insn_closure *dsc
-    = ((arm_displaced_step_copy_insn_closure * )
-	get_displaced_step_copy_insn_closure_by_addr (memaddr));
+  arm_displaced_step_copy_insn_closure *dsc = nullptr;
+
+  if (gdbarch_displaced_step_copy_insn_closure_by_addr_p (gdbarch))
+    dsc = ((arm_displaced_step_copy_insn_closure * )
+	   gdbarch_displaced_step_copy_insn_closure_by_addr
+	     (gdbarch, current_inferior (), memaddr));
 
   /* If checking the mode of displaced instruction in copy area, the mode
      should be determined by instruction on the original address.  */
diff --git a/gdb/bfin-linux-tdep.c b/gdb/bfin-linux-tdep.c
index 16a0a97eaf4..fc2f1d9ac65 100644
--- a/gdb/bfin-linux-tdep.c
+++ b/gdb/bfin-linux-tdep.c
@@ -150,7 +150,7 @@ bfin_linux_get_syscall_number (struct gdbarch *gdbarch,
 static void
 bfin_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   /* Set the sigtramp frame sniffer.  */
   tramp_frame_prepend_unwinder (gdbarch, &bfin_linux_sigframe);
diff --git a/gdb/cris-linux-tdep.c b/gdb/cris-linux-tdep.c
index 535dc4a1fce..85cbf4cc093 100644
--- a/gdb/cris-linux-tdep.c
+++ b/gdb/cris-linux-tdep.c
@@ -35,7 +35,7 @@ cris_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
 
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   if (tdep->cris_version == 32)
     /* Threaded debugging is only supported on CRISv32 for now.  */
diff --git a/gdb/csky-linux-tdep.c b/gdb/csky-linux-tdep.c
index fb431020446..184fa5ffb23 100644
--- a/gdb/csky-linux-tdep.c
+++ b/gdb/csky-linux-tdep.c
@@ -233,7 +233,7 @@ csky_linux_rt_sigreturn_tramp_frame = {
 static void
 csky_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   /* Shared library handling.  */
   set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
diff --git a/gdb/displaced-stepping.c b/gdb/displaced-stepping.c
index 5ae280fac39..f92eb52293f 100644
--- a/gdb/displaced-stepping.c
+++ b/gdb/displaced-stepping.c
@@ -19,9 +19,15 @@
 
 #include "defs.h"
 #include "displaced-stepping.h"
-#include "cli/cli-cmds.h"
 
+#include "cli/cli-cmds.h"
 #include "command.h"
+#include "gdbarch.h"
+#include "gdbcore.h"
+#include "gdbthread.h"
+#include "inferior.h"
+#include "regcache.h"
+#include "target/target.h"
 
 /* Default destructor for displaced_step_copy_insn_closure.  */
 
@@ -37,6 +43,188 @@ show_debug_displaced (struct ui_file *file, int from_tty,
   fprintf_filtered (file, _("Displace stepping debugging is %s.\n"), value);
 }
 
+displaced_step_prepare_status
+displaced_step_buffer::prepare (thread_info *thread, CORE_ADDR &displaced_pc)
+{
+  gdb_assert (!thread->displaced_step_state.in_progress ());
+
+  /* Is a thread currently using the buffer?  */
+  if (m_current_thread != nullptr)
+    {
+      /* If so, it better not be this thread.  */
+      gdb_assert (thread != m_current_thread);
+      return DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE;
+    }
+
+  regcache *regcache = get_thread_regcache (thread);
+  const address_space *aspace = regcache->aspace ();
+  gdbarch *arch = regcache->arch ();
+  ULONGEST len = gdbarch_max_insn_length (arch);
+
+  if (breakpoint_in_range_p (aspace, m_addr, len))
+    {
+      /* There's a breakpoint set in the scratch pad location range
+	 (which is usually around the entry point).  We'd either
+	 install it before resuming, which would overwrite/corrupt the
+	 scratch pad, or if it was already inserted, this displaced
+	 step would overwrite it.  The latter is OK in the sense that
+	 we already assume that no thread is going to execute the code
+	 in the scratch pad range (after initial startup) anyway, but
+	 the former is unacceptable.  Simply punt and fallback to
+	 stepping over this breakpoint in-line.  */
+      displaced_debug_printf ("breakpoint set in scratch pad.  "
+			      "Stepping over breakpoint in-line instead.");
+
+      return DISPLACED_STEP_PREPARE_STATUS_ERROR;
+    }
+
+  m_original_pc = regcache_read_pc (regcache);
+  displaced_pc = m_addr;
+
+  /* Save the original contents of the displaced stepping buffer.  */
+  m_saved_copy.resize (len);
+
+  int status = target_read_memory (m_addr, m_saved_copy.data (), len);
+  if (status != 0)
+    throw_error (MEMORY_ERROR,
+		 _("Error accessing memory address %s (%s) for "
+		   "displaced-stepping scratch space."),
+		 paddress (arch, m_addr), safe_strerror (status));
+
+  displaced_debug_printf ("saved %s: %s",
+			  paddress (arch, m_addr),
+			  displaced_step_dump_bytes
+			    (m_saved_copy.data (), len).c_str ());
+
+  m_copy_insn_closure = gdbarch_displaced_step_copy_insn (arch,
+							  m_original_pc,
+							  m_addr,
+							  regcache);
+  if (m_copy_insn_closure == nullptr)
+    {
+      /* The architecture doesn't know how or want to displaced step
+        this instruction or instruction sequence.  Fallback to
+        stepping over the breakpoint in-line.  */
+      return DISPLACED_STEP_PREPARE_STATUS_ERROR;
+    }
+
+  try
+    {
+      /* Resume execution at the copy.  */
+      regcache_write_pc (regcache, m_addr);
+    }
+  catch (...)
+    {
+      /* Failed to write the PC.  Release the architecture's displaced
+         stepping resources and the thread's displaced stepping state.  */
+      m_copy_insn_closure.reset ();
+
+      return DISPLACED_STEP_PREPARE_STATUS_ERROR;
+    }
+
+  /* This marks the buffer as being in use.  */
+  m_current_thread = thread;
+
+  return DISPLACED_STEP_PREPARE_STATUS_OK;
+}
+
+static void
+write_memory_ptid (ptid_t ptid, CORE_ADDR memaddr,
+		   const gdb_byte *myaddr, int len)
+{
+  scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
+
+  inferior_ptid = ptid;
+  write_memory (memaddr, myaddr, len);
+}
+
+static bool
+displaced_step_instruction_executed_successfully (gdbarch *arch,
+						   gdb_signal signal)
+{
+  if (signal != GDB_SIGNAL_TRAP)
+    return false;
+
+  if (target_stopped_by_watchpoint ())
+    {
+      if (gdbarch_have_nonsteppable_watchpoint (arch)
+	  || target_have_steppable_watchpoint ())
+	return false;
+    }
+
+  return true;
+}
+
+displaced_step_finish_status
+displaced_step_buffer::finish (gdbarch *arch, thread_info *thread,
+			       gdb_signal sig)
+{
+  gdb_assert (thread->displaced_step_state.in_progress ());
+  gdb_assert (thread == m_current_thread);
+
+  ULONGEST len = gdbarch_max_insn_length (arch);
+
+  write_memory_ptid (thread->ptid, m_addr,
+		     m_saved_copy.data (), len);
+
+  displaced_debug_printf ("restored %s %s",
+			  target_pid_to_str (thread->ptid).c_str (),
+			  paddress (arch, m_addr));
+
+  regcache *rc = get_thread_regcache (thread);
+
+  bool instruction_executed_successfully
+    = displaced_step_instruction_executed_successfully (arch, sig);
+  displaced_step_finish_status status;
+
+  if (instruction_executed_successfully)
+    {
+      gdbarch_displaced_step_fixup (arch, m_copy_insn_closure.get (), m_original_pc,
+				    m_addr, rc);
+      status = DISPLACED_STEP_FINISH_STATUS_OK;
+    }
+  else
+    {
+      /* Since the instruction didn't complete, all we can do is relocate the
+	 PC.  */
+      CORE_ADDR pc = regcache_read_pc (rc);
+      pc = m_original_pc + (pc - m_addr);
+      regcache_write_pc (rc, pc);
+      status = DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED;
+    }
+
+  m_copy_insn_closure.reset ();
+  m_current_thread = nullptr;
+
+  return status;
+}
+
+const displaced_step_copy_insn_closure *
+displaced_step_buffer::copy_insn_closure_by_addr (CORE_ADDR addr)
+{
+  if (addr == m_addr)
+    return m_copy_insn_closure.get ();
+  else
+    return nullptr;
+}
+
+void
+displaced_step_buffer::restore_in_ptid (ptid_t ptid)
+{
+  if (m_current_thread != nullptr)
+    {
+      regcache *regcache = get_thread_regcache (m_current_thread);
+      gdbarch *arch = regcache->arch ();
+      ULONGEST len = gdbarch_max_insn_length (arch);
+
+      write_memory_ptid (ptid, m_addr, m_saved_copy.data (), len);
+
+      displaced_debug_printf ("restored in ptid %s %s",
+			      target_pid_to_str (ptid).c_str (),
+			      paddress (arch, m_addr));
+    }
+}
+
 void _initialize_displaced_stepping ();
 void
 _initialize_displaced_stepping ()
diff --git a/gdb/displaced-stepping.h b/gdb/displaced-stepping.h
index 99343f84e44..6c1da46777c 100644
--- a/gdb/displaced-stepping.h
+++ b/gdb/displaced-stepping.h
@@ -22,6 +22,7 @@
 
 #include "gdbsupport/byte-vector.h"
 
+struct gdbarch;
 struct thread_info;
 
 /* True if we are debugging displaced stepping.  */
@@ -61,7 +62,8 @@ enum displaced_step_finish_status
   DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED,
 };
 
-/* Base class for displaced stepping closures (the arch-specific data).  */
+/* Data returned by a gdbarch displaced_step_copy_insn method, to be passed to
+   the matching displaced_step_fixup method.  */
 
 struct displaced_step_copy_insn_closure
 {
@@ -79,6 +81,9 @@ struct buf_displaced_step_copy_insn_closure : displaced_step_copy_insn_closure
   : buf (buf_size)
   {}
 
+  /* The content of this buffer is up to the user of the class, but typically
+     original instruction bytes, used during fixup to determine what needs to
+     be fixed up.  */
   gdb::byte_vector buf;
 };
 
@@ -94,37 +99,78 @@ struct displaced_step_inferior_state
   /* Put this object back in its original state.  */
   void reset ()
   {
-    failed_before = 0;
-    step_thread = nullptr;
-    step_gdbarch = nullptr;
-    step_closure.reset ();
-    step_original = 0;
-    step_copy = 0;
-    step_saved_copy.clear ();
+    failed_before = false;
   }
 
   /* True if preparing a displaced step ever failed.  If so, we won't
      try displaced stepping for this inferior again.  */
-  int failed_before;
+  bool failed_before;
+};
+
+/* Per-thread displaced stepping state.  */
+
+struct displaced_step_thread_state
+{
+  /* Return true if this thread is currently executing a displaced step.  */
+  bool in_progress () const
+  {
+    return m_original_gdbarch != nullptr;
+  }
+
+  /* Return the gdbarch of the thread prior to the step.  */
+  gdbarch *get_original_gdbarch () const
+  {
+    return m_original_gdbarch;
+  }
+
+  /* Mark this thread as currently executing a displaced step.
+
+     ORIGINAL_GDBARCH is the current gdbarch of the thread (before the step
+     is executed).  */
+  void set (gdbarch *original_gdbarch)
+  {
+    m_original_gdbarch = original_gdbarch;
+  }
+
+  /* Mark this thread as no longer executing a displaced step.  */
+  void reset ()
+  {
+    m_original_gdbarch = nullptr;
+  }
+
+private:
+  gdbarch *m_original_gdbarch = nullptr;
+};
+
+/* Manage access to a single displaced stepping buffer.  */
+
+struct displaced_step_buffer
+{
+  displaced_step_buffer (CORE_ADDR buffer_addr)
+    : m_addr (buffer_addr)
+  {}
+
+  displaced_step_prepare_status prepare (thread_info *thread,
+					 CORE_ADDR &displaced_pc);
+
+  displaced_step_finish_status finish (gdbarch *arch, thread_info *thread,
+				       gdb_signal sig);
+
+  const displaced_step_copy_insn_closure *
+    copy_insn_closure_by_addr (CORE_ADDR addr);
 
-  /* If this is not nullptr, this is the thread carrying out a
-     displaced single-step in process PID.  This thread's state will
-     require fixing up once it has completed its step.  */
-  thread_info *step_thread;
+  void restore_in_ptid (ptid_t ptid);
 
-  /* The architecture the thread had when we stepped it.  */
-  gdbarch *step_gdbarch;
+private:
 
-  /* The closure provided gdbarch_displaced_step_copy_insn, to be used
-     for post-step cleanup.  */
-  displaced_step_copy_insn_closure_up step_closure;
+  CORE_ADDR m_original_pc = 0;
+  const CORE_ADDR m_addr;
 
-  /* The address of the original instruction, and the copy we
-     made.  */
-  CORE_ADDR step_original, step_copy;
+  /* If set, the thread currently using the buffer.  */
+  thread_info *m_current_thread = nullptr;
 
-  /* Saved contents of copy area.  */
-  gdb::byte_vector step_saved_copy;
+  gdb::byte_vector m_saved_copy;
+  displaced_step_copy_insn_closure_up m_copy_insn_closure;
 };
 
 #endif /* DISPLACED_STEPPING_H */
diff --git a/gdb/frv-linux-tdep.c b/gdb/frv-linux-tdep.c
index 2dc18206975..c5ae4212ab4 100644
--- a/gdb/frv-linux-tdep.c
+++ b/gdb/frv-linux-tdep.c
@@ -456,7 +456,7 @@ frv_linux_iterate_over_regset_sections (struct gdbarch *gdbarch,
 static void
 frv_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   /* Set the sigtramp frame sniffer.  */
   frame_unwind_append_unwinder (gdbarch, &frv_linux_sigtramp_frame_unwind); 
diff --git a/gdb/gdbarch.c b/gdb/gdbarch.c
index 3da77416d6e..d319d0354ae 100644
--- a/gdb/gdbarch.c
+++ b/gdb/gdbarch.c
@@ -290,7 +290,10 @@ struct gdbarch
   gdbarch_displaced_step_copy_insn_ftype *displaced_step_copy_insn;
   gdbarch_displaced_step_hw_singlestep_ftype *displaced_step_hw_singlestep;
   gdbarch_displaced_step_fixup_ftype *displaced_step_fixup;
-  gdbarch_displaced_step_location_ftype *displaced_step_location;
+  gdbarch_displaced_step_prepare_ftype *displaced_step_prepare;
+  gdbarch_displaced_step_finish_ftype *displaced_step_finish;
+  gdbarch_displaced_step_copy_insn_closure_by_addr_ftype *displaced_step_copy_insn_closure_by_addr;
+  gdbarch_displaced_step_restore_all_in_ptid_ftype *displaced_step_restore_all_in_ptid;
   gdbarch_relocate_instruction_ftype *relocate_instruction;
   gdbarch_overlay_update_ftype *overlay_update;
   gdbarch_core_read_description_ftype *core_read_description;
@@ -445,7 +448,7 @@ gdbarch_alloc (const struct gdbarch_info *info,
   gdbarch->skip_permanent_breakpoint = default_skip_permanent_breakpoint;
   gdbarch->displaced_step_hw_singlestep = default_displaced_step_hw_singlestep;
   gdbarch->displaced_step_fixup = NULL;
-  gdbarch->displaced_step_location = NULL;
+  gdbarch->displaced_step_finish = NULL;
   gdbarch->relocate_instruction = NULL;
   gdbarch->has_shared_address_space = default_has_shared_address_space;
   gdbarch->fast_tracepoint_valid_at = default_fast_tracepoint_valid_at;
@@ -660,8 +663,10 @@ verify_gdbarch (struct gdbarch *gdbarch)
   /* Skip verify of displaced_step_copy_insn, has predicate.  */
   /* Skip verify of displaced_step_hw_singlestep, invalid_p == 0 */
   /* Skip verify of displaced_step_fixup, has predicate.  */
-  if ((! gdbarch->displaced_step_location) != (! gdbarch->displaced_step_copy_insn))
-    log.puts ("\n\tdisplaced_step_location");
+  /* Skip verify of displaced_step_prepare, has predicate.  */
+  if ((! gdbarch->displaced_step_finish) != (! gdbarch->displaced_step_prepare))
+    log.puts ("\n\tdisplaced_step_finish");
+  /* Skip verify of displaced_step_copy_insn_closure_by_addr, has predicate.  */
   /* Skip verify of relocate_instruction, has predicate.  */
   /* Skip verify of overlay_update, has predicate.  */
   /* Skip verify of core_read_description, has predicate.  */
@@ -924,6 +929,15 @@ gdbarch_dump (struct gdbarch *gdbarch, struct ui_file *file)
   fprintf_unfiltered (file,
                       "gdbarch_dump: displaced_step_copy_insn = <%s>\n",
                       host_address_to_string (gdbarch->displaced_step_copy_insn));
+  fprintf_unfiltered (file,
+                      "gdbarch_dump: gdbarch_displaced_step_copy_insn_closure_by_addr_p() = %d\n",
+                      gdbarch_displaced_step_copy_insn_closure_by_addr_p (gdbarch));
+  fprintf_unfiltered (file,
+                      "gdbarch_dump: displaced_step_copy_insn_closure_by_addr = <%s>\n",
+                      host_address_to_string (gdbarch->displaced_step_copy_insn_closure_by_addr));
+  fprintf_unfiltered (file,
+                      "gdbarch_dump: displaced_step_finish = <%s>\n",
+                      host_address_to_string (gdbarch->displaced_step_finish));
   fprintf_unfiltered (file,
                       "gdbarch_dump: gdbarch_displaced_step_fixup_p() = %d\n",
                       gdbarch_displaced_step_fixup_p (gdbarch));
@@ -934,8 +948,14 @@ gdbarch_dump (struct gdbarch *gdbarch, struct ui_file *file)
                       "gdbarch_dump: displaced_step_hw_singlestep = <%s>\n",
                       host_address_to_string (gdbarch->displaced_step_hw_singlestep));
   fprintf_unfiltered (file,
-                      "gdbarch_dump: displaced_step_location = <%s>\n",
-                      host_address_to_string (gdbarch->displaced_step_location));
+                      "gdbarch_dump: gdbarch_displaced_step_prepare_p() = %d\n",
+                      gdbarch_displaced_step_prepare_p (gdbarch));
+  fprintf_unfiltered (file,
+                      "gdbarch_dump: displaced_step_prepare = <%s>\n",
+                      host_address_to_string (gdbarch->displaced_step_prepare));
+  fprintf_unfiltered (file,
+                      "gdbarch_dump: displaced_step_restore_all_in_ptid = <%s>\n",
+                      host_address_to_string (gdbarch->displaced_step_restore_all_in_ptid));
   fprintf_unfiltered (file,
                       "gdbarch_dump: double_bit = %s\n",
                       plongest (gdbarch->double_bit));
@@ -4028,21 +4048,86 @@ set_gdbarch_displaced_step_fixup (struct gdbarch *gdbarch,
   gdbarch->displaced_step_fixup = displaced_step_fixup;
 }
 
-CORE_ADDR
-gdbarch_displaced_step_location (struct gdbarch *gdbarch)
+bool
+gdbarch_displaced_step_prepare_p (struct gdbarch *gdbarch)
+{
+  gdb_assert (gdbarch != NULL);
+  return gdbarch->displaced_step_prepare != NULL;
+}
+
+displaced_step_prepare_status
+gdbarch_displaced_step_prepare (struct gdbarch *gdbarch, thread_info *thread, CORE_ADDR &displaced_pc)
+{
+  gdb_assert (gdbarch != NULL);
+  gdb_assert (gdbarch->displaced_step_prepare != NULL);
+  if (gdbarch_debug >= 2)
+    fprintf_unfiltered (gdb_stdlog, "gdbarch_displaced_step_prepare called\n");
+  return gdbarch->displaced_step_prepare (gdbarch, thread, displaced_pc);
+}
+
+void
+set_gdbarch_displaced_step_prepare (struct gdbarch *gdbarch,
+                                    gdbarch_displaced_step_prepare_ftype displaced_step_prepare)
+{
+  gdbarch->displaced_step_prepare = displaced_step_prepare;
+}
+
+displaced_step_finish_status
+gdbarch_displaced_step_finish (struct gdbarch *gdbarch, thread_info *thread, gdb_signal sig)
+{
+  gdb_assert (gdbarch != NULL);
+  gdb_assert (gdbarch->displaced_step_finish != NULL);
+  if (gdbarch_debug >= 2)
+    fprintf_unfiltered (gdb_stdlog, "gdbarch_displaced_step_finish called\n");
+  return gdbarch->displaced_step_finish (gdbarch, thread, sig);
+}
+
+void
+set_gdbarch_displaced_step_finish (struct gdbarch *gdbarch,
+                                   gdbarch_displaced_step_finish_ftype displaced_step_finish)
+{
+  gdbarch->displaced_step_finish = displaced_step_finish;
+}
+
+bool
+gdbarch_displaced_step_copy_insn_closure_by_addr_p (struct gdbarch *gdbarch)
+{
+  gdb_assert (gdbarch != NULL);
+  return gdbarch->displaced_step_copy_insn_closure_by_addr != NULL;
+}
+
+const displaced_step_copy_insn_closure *
+gdbarch_displaced_step_copy_insn_closure_by_addr (struct gdbarch *gdbarch, inferior *inf, CORE_ADDR addr)
+{
+  gdb_assert (gdbarch != NULL);
+  gdb_assert (gdbarch->displaced_step_copy_insn_closure_by_addr != NULL);
+  if (gdbarch_debug >= 2)
+    fprintf_unfiltered (gdb_stdlog, "gdbarch_displaced_step_copy_insn_closure_by_addr called\n");
+  return gdbarch->displaced_step_copy_insn_closure_by_addr (inf, addr);
+}
+
+void
+set_gdbarch_displaced_step_copy_insn_closure_by_addr (struct gdbarch *gdbarch,
+                                                      gdbarch_displaced_step_copy_insn_closure_by_addr_ftype displaced_step_copy_insn_closure_by_addr)
+{
+  gdbarch->displaced_step_copy_insn_closure_by_addr = displaced_step_copy_insn_closure_by_addr;
+}
+
+void
+gdbarch_displaced_step_restore_all_in_ptid (struct gdbarch *gdbarch, inferior *parent_inf, ptid_t child_ptid)
 {
   gdb_assert (gdbarch != NULL);
-  gdb_assert (gdbarch->displaced_step_location != NULL);
+  gdb_assert (gdbarch->displaced_step_restore_all_in_ptid != NULL);
   if (gdbarch_debug >= 2)
-    fprintf_unfiltered (gdb_stdlog, "gdbarch_displaced_step_location called\n");
-  return gdbarch->displaced_step_location (gdbarch);
+    fprintf_unfiltered (gdb_stdlog, "gdbarch_displaced_step_restore_all_in_ptid called\n");
+  gdbarch->displaced_step_restore_all_in_ptid (parent_inf, child_ptid);
 }
 
 void
-set_gdbarch_displaced_step_location (struct gdbarch *gdbarch,
-                                     gdbarch_displaced_step_location_ftype displaced_step_location)
+set_gdbarch_displaced_step_restore_all_in_ptid (struct gdbarch *gdbarch,
+                                                gdbarch_displaced_step_restore_all_in_ptid_ftype displaced_step_restore_all_in_ptid)
 {
-  gdbarch->displaced_step_location = displaced_step_location;
+  gdbarch->displaced_step_restore_all_in_ptid = displaced_step_restore_all_in_ptid;
 }
 
 bool
diff --git a/gdb/gdbarch.h b/gdb/gdbarch.h
index 883f8fb3329..4ca64ee580a 100644
--- a/gdb/gdbarch.h
+++ b/gdb/gdbarch.h
@@ -58,6 +58,7 @@ struct mem_range;
 struct syscalls_info;
 struct thread_info;
 struct ui_out;
+struct inferior;
 
 #include "regcache.h"
 
@@ -1071,17 +1072,34 @@ typedef void (gdbarch_displaced_step_fixup_ftype) (struct gdbarch *gdbarch, stru
 extern void gdbarch_displaced_step_fixup (struct gdbarch *gdbarch, struct displaced_step_copy_insn_closure *closure, CORE_ADDR from, CORE_ADDR to, struct regcache *regs);
 extern void set_gdbarch_displaced_step_fixup (struct gdbarch *gdbarch, gdbarch_displaced_step_fixup_ftype *displaced_step_fixup);
 
-/* Return the address of an appropriate place to put displaced
-   instructions while we step over them.  There need only be one such
-   place, since we're only stepping one thread over a breakpoint at a
-   time.
-  
-   For a general explanation of displaced stepping and how GDB uses it,
-   see the comments in infrun.c. */
+/* Prepare THREAD for it to displaced step the instruction at its current PC. */
+
+extern bool gdbarch_displaced_step_prepare_p (struct gdbarch *gdbarch);
+
+typedef displaced_step_prepare_status (gdbarch_displaced_step_prepare_ftype) (struct gdbarch *gdbarch, thread_info *thread, CORE_ADDR &displaced_pc);
+extern displaced_step_prepare_status gdbarch_displaced_step_prepare (struct gdbarch *gdbarch, thread_info *thread, CORE_ADDR &displaced_pc);
+extern void set_gdbarch_displaced_step_prepare (struct gdbarch *gdbarch, gdbarch_displaced_step_prepare_ftype *displaced_step_prepare);
+
+/* Clean up after a displaced step of THREAD. */
+
+typedef displaced_step_finish_status (gdbarch_displaced_step_finish_ftype) (struct gdbarch *gdbarch, thread_info *thread, gdb_signal sig);
+extern displaced_step_finish_status gdbarch_displaced_step_finish (struct gdbarch *gdbarch, thread_info *thread, gdb_signal sig);
+extern void set_gdbarch_displaced_step_finish (struct gdbarch *gdbarch, gdbarch_displaced_step_finish_ftype *displaced_step_finish);
+
+/* Return the closure associated to the displaced step buffer that is at ADDR. */
+
+extern bool gdbarch_displaced_step_copy_insn_closure_by_addr_p (struct gdbarch *gdbarch);
+
+typedef const displaced_step_copy_insn_closure * (gdbarch_displaced_step_copy_insn_closure_by_addr_ftype) (inferior *inf, CORE_ADDR addr);
+extern const displaced_step_copy_insn_closure * gdbarch_displaced_step_copy_insn_closure_by_addr (struct gdbarch *gdbarch, inferior *inf, CORE_ADDR addr);
+extern void set_gdbarch_displaced_step_copy_insn_closure_by_addr (struct gdbarch *gdbarch, gdbarch_displaced_step_copy_insn_closure_by_addr_ftype *displaced_step_copy_insn_closure_by_addr);
+
+/* PARENT_INF has forked and CHILD_PTID is the ptid of the child.  Restore the
+   contents of all displaced step buffers in the child's address space. */
 
-typedef CORE_ADDR (gdbarch_displaced_step_location_ftype) (struct gdbarch *gdbarch);
-extern CORE_ADDR gdbarch_displaced_step_location (struct gdbarch *gdbarch);
-extern void set_gdbarch_displaced_step_location (struct gdbarch *gdbarch, gdbarch_displaced_step_location_ftype *displaced_step_location);
+typedef void (gdbarch_displaced_step_restore_all_in_ptid_ftype) (inferior *parent_inf, ptid_t child_ptid);
+extern void gdbarch_displaced_step_restore_all_in_ptid (struct gdbarch *gdbarch, inferior *parent_inf, ptid_t child_ptid);
+extern void set_gdbarch_displaced_step_restore_all_in_ptid (struct gdbarch *gdbarch, gdbarch_displaced_step_restore_all_in_ptid_ftype *displaced_step_restore_all_in_ptid);
 
 /* Relocate an instruction to execute at a different address.  OLDLOC
    is the address in the inferior memory where the instruction to
diff --git a/gdb/gdbarch.sh b/gdb/gdbarch.sh
index 47007807c1e..a3f88047159 100755
--- a/gdb/gdbarch.sh
+++ b/gdb/gdbarch.sh
@@ -813,14 +813,18 @@ m;bool;displaced_step_hw_singlestep;void;;;default_displaced_step_hw_singlestep;
 # see the comments in infrun.c.
 M;void;displaced_step_fixup;struct displaced_step_copy_insn_closure *closure, CORE_ADDR from, CORE_ADDR to, struct regcache *regs;closure, from, to, regs;;NULL
 
-# Return the address of an appropriate place to put displaced
-# instructions while we step over them.  There need only be one such
-# place, since we're only stepping one thread over a breakpoint at a
-# time.
-#
-# For a general explanation of displaced stepping and how GDB uses it,
-# see the comments in infrun.c.
-m;CORE_ADDR;displaced_step_location;void;;;NULL;;(! gdbarch->displaced_step_location) != (! gdbarch->displaced_step_copy_insn)
+# Prepare THREAD for it to displaced step the instruction at its current PC.
+M;displaced_step_prepare_status;displaced_step_prepare;thread_info *thread, CORE_ADDR &displaced_pc;thread, displaced_pc
+
+# Clean up after a displaced step of THREAD.
+m;displaced_step_finish_status;displaced_step_finish;thread_info *thread, gdb_signal sig;thread, sig;;NULL;;(! gdbarch->displaced_step_finish) != (! gdbarch->displaced_step_prepare)
+
+# Return the closure associated to the displaced step buffer that is at ADDR.
+F;const displaced_step_copy_insn_closure *;displaced_step_copy_insn_closure_by_addr;inferior *inf, CORE_ADDR addr;inf, addr
+
+# PARENT_INF has forked and CHILD_PTID is the ptid of the child.  Restore the
+# contents of all displaced step buffers in the child's address space.
+f;void;displaced_step_restore_all_in_ptid;inferior *parent_inf, ptid_t child_ptid;parent_inf, child_ptid
 
 # Relocate an instruction to execute at a different address.  OLDLOC
 # is the address in the inferior memory where the instruction to
@@ -1297,6 +1301,7 @@ struct mem_range;
 struct syscalls_info;
 struct thread_info;
 struct ui_out;
+struct inferior;
 
 #include "regcache.h"
 
diff --git a/gdb/gdbthread.h b/gdb/gdbthread.h
index b5a32087289..71dc14711be 100644
--- a/gdb/gdbthread.h
+++ b/gdb/gdbthread.h
@@ -32,6 +32,7 @@ struct symtab;
 #include "gdbsupport/refcounted-object.h"
 #include "gdbsupport/common-gdbthread.h"
 #include "gdbsupport/forward-scope-exit.h"
+#include "displaced-stepping.h"
 
 struct inferior;
 struct process_stratum_target;
@@ -388,6 +389,9 @@ class thread_info : public refcounted_object
      fields point to self.  */
   struct thread_info *step_over_prev = NULL;
   struct thread_info *step_over_next = NULL;
+
+  /* Displaced-step state for this thread.  */
+  displaced_step_thread_state displaced_step_state;
 };
 
 /* A gdb::ref_ptr pointer to a thread_info.  */
@@ -745,10 +749,21 @@ extern bool value_in_thread_stack_temporaries (struct value *,
 
 extern void global_thread_step_over_chain_enqueue (struct thread_info *tp);
 
+/* Remove TP from step-over chain LIST_P.  */
+
+extern void thread_step_over_chain_remove (thread_info **list_p,
+					   thread_info *tp);
+
 /* Remove TP from the global pending step-over chain.  */
 
 extern void global_thread_step_over_chain_remove (struct thread_info *tp);
 
+/* Return the thread following TP in the step-over chain whose head is
+   CHAIN_HEAD.  Return NULL if TP is the last entry in the chain.  */
+
+extern thread_info *thread_step_over_chain_next (thread_info *chain_head,
+						 thread_info *tp);
+
 /* Return the thread following TP in the global step-over chain, or NULL if TP
    is the last entry in the chain.  */
 
@@ -758,6 +773,10 @@ extern struct thread_info *global_thread_step_over_chain_next (struct thread_inf
 
 extern int thread_is_in_step_over_chain (struct thread_info *tp);
 
+/* Return the length of the the step over chain TP is in.  */
+
+extern int thread_step_over_chain_length (thread_info *tp);
+
 /* Cancel any ongoing execution command.  */
 
 extern void thread_cancel_execution_command (struct thread_info *thr);
diff --git a/gdb/hppa-linux-tdep.c b/gdb/hppa-linux-tdep.c
index 080379992ae..a171e582e59 100644
--- a/gdb/hppa-linux-tdep.c
+++ b/gdb/hppa-linux-tdep.c
@@ -489,7 +489,7 @@ hppa_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
 
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   /* GNU/Linux is always ELF.  */
   tdep->is_elf = 1;
diff --git a/gdb/i386-linux-tdep.c b/gdb/i386-linux-tdep.c
index ab7d23611f8..1b209fd3eff 100644
--- a/gdb/i386-linux-tdep.c
+++ b/gdb/i386-linux-tdep.c
@@ -832,7 +832,7 @@ i386_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 
   gdb_assert (tdesc_data);
 
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, true);
 
   /* GNU/Linux uses ELF.  */
   i386_elf_init_abi (info, gdbarch);
@@ -1065,8 +1065,6 @@ i386_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
   set_gdbarch_displaced_step_copy_insn (gdbarch,
 					i386_linux_displaced_step_copy_insn);
   set_gdbarch_displaced_step_fixup (gdbarch, i386_displaced_step_fixup);
-  set_gdbarch_displaced_step_location (gdbarch,
-				       linux_displaced_step_location);
 
   /* Functions for 'catch syscall'.  */
   set_xml_syscall_file_name (gdbarch, XML_SYSCALL_FILENAME_I386);
diff --git a/gdb/ia64-linux-tdep.c b/gdb/ia64-linux-tdep.c
index 587a455467d..d6d581ab3dd 100644
--- a/gdb/ia64-linux-tdep.c
+++ b/gdb/ia64-linux-tdep.c
@@ -223,7 +223,7 @@ ia64_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
   static const char *const stap_register_indirection_suffixes[] = { "]",
 								    NULL };
 
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   /* Set the method of obtaining the sigcontext addresses at which
      registers are saved.  */
diff --git a/gdb/infrun.c b/gdb/infrun.c
index d09209cd8d4..27538dc982c 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -1460,19 +1460,12 @@ get_displaced_stepping_state (inferior *inf)
   return &inf->displaced_step_state;
 }
 
-/* Returns true if any inferior has a thread doing a displaced
-   step.  */
+/* Get the displaced stepping state of thread THREAD.  */
 
-static bool
-displaced_step_in_progress_any_inferior ()
+static displaced_step_thread_state *
+get_displaced_stepping_state (thread_info *thread)
 {
-  for (inferior *i : all_inferiors ())
-    {
-      if (i->displaced_step_state.step_thread != nullptr)
-	return true;
-    }
-
-  return false;
+  return &thread->displaced_step_state;
 }
 
 /* Return true if THREAD is doing a displaced step.  */
@@ -1482,7 +1475,7 @@ displaced_step_in_progress_thread (thread_info *thread)
 {
   gdb_assert (thread != NULL);
 
-  return get_displaced_stepping_state (thread->inf)->step_thread == thread;
+  return get_displaced_stepping_state (thread)->in_progress ();
 }
 
 /* Return true if INF has a thread doing a displaced step.  */
@@ -1490,25 +1483,27 @@ displaced_step_in_progress_thread (thread_info *thread)
 static bool
 displaced_step_in_progress (inferior *inf)
 {
-  return get_displaced_stepping_state (inf)->step_thread != nullptr;
+  for (thread_info *thread : inf->non_exited_threads ())
+    {
+      if (displaced_step_in_progress_thread (thread))
+	return true;
+    }
+
+  return false;
 }
 
-/* If inferior is in displaced stepping, and ADDR equals to starting address
-   of copy area, return corresponding displaced_step_copy_insn_closure.
-   Otherwise, return NULL.  */
+/* Return true if any thread is doing a displaced step.  */
 
-displaced_step_copy_insn_closure *
-get_displaced_step_copy_insn_closure_by_addr (CORE_ADDR addr)
+static bool
+displaced_step_in_progress_any_thread ()
 {
-  displaced_step_inferior_state *displaced
-    = get_displaced_stepping_state (current_inferior ());
-
-  /* If checking the mode of displaced instruction in copy area.  */
-  if (displaced->step_thread != nullptr
-      && displaced->step_copy == addr)
-    return displaced->step_closure.get ();
+  for (thread_info *thread : all_non_exited_threads ())
+    {
+      if (displaced_step_in_progress_thread (thread))
+	return true;
+    }
 
-  return NULL;
+  return false;
 }
 
 static void
@@ -1554,9 +1549,9 @@ show_can_use_displaced_stepping (struct ui_file *file, int from_tty,
 static bool
 gdbarch_supports_displaced_stepping (gdbarch *arch)
 {
-  /* Only check for the presence of step_copy_insn.  Other required methods
-     are checked by the gdbarch validation.  */
-  return gdbarch_displaced_step_copy_insn_p (arch);
+  /* Only check for the presence of `prepare`.  `finish` is required by the
+     gdbarch verification to be provided if `prepare` is provided.  */
+  return gdbarch_displaced_step_prepare_p (arch);
 }
 
 /* Return non-zero if displaced stepping can/should be used to step
@@ -1597,10 +1592,10 @@ use_displaced_stepping (thread_info *tp)
   return true;
 }
 
-/* Simple function wrapper around displaced_step_inferior_state::reset.  */
+/* Simple function wrapper around displaced_step_thread_state::reset.  */
 
 static void
-displaced_step_reset (displaced_step_inferior_state *displaced)
+displaced_step_reset (displaced_step_thread_state *displaced)
 {
   displaced->reset ();
 }
@@ -1651,10 +1646,8 @@ displaced_step_prepare_throw (thread_info *tp)
 {
   regcache *regcache = get_thread_regcache (tp);
   struct gdbarch *gdbarch = regcache->arch ();
-  const address_space *aspace = regcache->aspace ();
-  CORE_ADDR original, copy;
-  ULONGEST len;
-  int status;
+  displaced_step_thread_state *disp_step_thread_state
+    = get_displaced_stepping_state (tp);
 
   /* We should never reach this function if the architecture does not
      support displaced stepping.  */
@@ -1669,96 +1662,52 @@ displaced_step_prepare_throw (thread_info *tp)
      jump/branch).  */
   tp->control.may_range_step = 0;
 
-  /* We have to displaced step one thread at a time, as we only have
-     access to a single scratch space per inferior.  */
-
-  displaced_step_inferior_state *displaced
-    = get_displaced_stepping_state (tp->inf);
-
-  if (displaced->step_thread != nullptr)
-    {
-      /* Already waiting for a displaced step to finish.  Defer this
-	 request and place in queue.  */
-
-      displaced_debug_printf ("deferring step of %s",
-			      target_pid_to_str (tp->ptid).c_str ());
-
-      global_thread_step_over_chain_enqueue (tp);
-      return DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE;
-    }
-  else
-    displaced_debug_printf ("stepping %s now",
-			    target_pid_to_str (tp->ptid).c_str ());
-
-  displaced_step_reset (displaced);
+  /* We are about to start a displaced step for this thread.  If one is already
+     in progress, something's wrong..  */
+  gdb_assert (!disp_step_thread_state->in_progress ());
 
   scoped_restore_current_thread restore_thread;
 
   switch_to_thread (tp);
 
-  original = regcache_read_pc (regcache);
+  CORE_ADDR original_pc = regcache_read_pc (regcache);
+  CORE_ADDR displaced_pc;
 
-  copy = gdbarch_displaced_step_location (gdbarch);
-  len = gdbarch_max_insn_length (gdbarch);
+  displaced_step_prepare_status status =
+    gdbarch_displaced_step_prepare (gdbarch, tp, displaced_pc);
 
-  if (breakpoint_in_range_p (aspace, copy, len))
+  if (status == DISPLACED_STEP_PREPARE_STATUS_ERROR)
     {
-      /* There's a breakpoint set in the scratch pad location range
-	 (which is usually around the entry point).  We'd either
-	 install it before resuming, which would overwrite/corrupt the
-	 scratch pad, or if it was already inserted, this displaced
-	 step would overwrite it.  The latter is OK in the sense that
-	 we already assume that no thread is going to execute the code
-	 in the scratch pad range (after initial startup) anyway, but
-	 the former is unacceptable.  Simply punt and fallback to
-	 stepping over this breakpoint in-line.  */
-      displaced_debug_printf ("breakpoint set in scratch pad.  "
-			      "Stepping over breakpoint in-line instead.");
+      displaced_debug_printf ("failed to prepare (%s)",
+			      target_pid_to_str (tp->ptid).c_str ());
 
       return DISPLACED_STEP_PREPARE_STATUS_ERROR;
     }
+  else if (status == DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE)
+    {
+      /* Not enough displaced stepping resources available, defer this
+	 request by placing it the queue.  */
 
-  /* Save the original contents of the copy area.  */
-  displaced->step_saved_copy.resize (len);
-  status = target_read_memory (copy, displaced->step_saved_copy.data (), len);
-  if (status != 0)
-    throw_error (MEMORY_ERROR,
-		 _("Error accessing memory address %s (%s) for "
-		   "displaced-stepping scratch space."),
-		 paddress (gdbarch, copy), safe_strerror (status));
+      displaced_debug_printf ("not enough resources available, "
+			      "deferring step of %s",
+			      target_pid_to_str (tp->ptid).c_str ());
 
-  displaced_debug_printf ("saved %s: %s",
-			  paddress (gdbarch, copy),
-			  displaced_step_dump_bytes
-			    (displaced->step_saved_copy.data (), len).c_str ());
+      global_thread_step_over_chain_enqueue (tp);
 
-  displaced->step_closure
-    = gdbarch_displaced_step_copy_insn (gdbarch, original, copy, regcache);
-  if (displaced->step_closure == NULL)
-    {
-      /* The architecture doesn't know how or want to displaced step
-	 this instruction or instruction sequence.  Fallback to
-	 stepping over the breakpoint in-line.  */
-      return DISPLACED_STEP_PREPARE_STATUS_ERROR;
+      return DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE;
     }
 
+  gdb_assert (status == DISPLACED_STEP_PREPARE_STATUS_OK);
+
   /* Save the information we need to fix things up if the step
      succeeds.  */
-  displaced->step_thread = tp;
-  displaced->step_gdbarch = gdbarch;
-  displaced->step_original = original;
-  displaced->step_copy = copy;
-
-  {
-    displaced_step_reset_cleanup cleanup (displaced);
-
-    /* Resume execution at the copy.  */
-    regcache_write_pc (regcache, copy);
-
-    cleanup.release ();
-  }
+  disp_step_thread_state->set (gdbarch);
 
-  displaced_debug_printf ("displaced pc to %s", paddress (gdbarch, copy));
+  displaced_debug_printf ("prepared successfully thread=%s, "
+			  "original_pc=%s, displaced_pc=%s",
+			  target_pid_to_str (tp->ptid).c_str (),
+			  paddress (gdbarch, original_pc),
+			  paddress (gdbarch, displaced_pc));
 
   return DISPLACED_STEP_PREPARE_STATUS_OK;
 }
@@ -1804,33 +1753,6 @@ displaced_step_prepare (thread_info *thread)
   return status;
 }
 
-static void
-write_memory_ptid (ptid_t ptid, CORE_ADDR memaddr,
-		   const gdb_byte *myaddr, int len)
-{
-  scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
-
-  inferior_ptid = ptid;
-  write_memory (memaddr, myaddr, len);
-}
-
-/* Restore the contents of the copy area for thread PTID.  */
-
-static void
-displaced_step_restore (struct displaced_step_inferior_state *displaced,
-			ptid_t ptid)
-{
-  ULONGEST len = gdbarch_max_insn_length (displaced->step_gdbarch);
-
-  write_memory_ptid (ptid, displaced->step_copy,
-		     displaced->step_saved_copy.data (), len);
-
-  displaced_debug_printf ("restored %s %s",
-			  target_pid_to_str (ptid).c_str (),
-			  paddress (displaced->step_gdbarch,
-				    displaced->step_copy));
-}
-
 /* If we displaced stepped an instruction successfully, adjust registers and
    memory to yield the same effect the instruction would have had if we had
    executed it at its original address, and return
@@ -1843,11 +1765,11 @@ displaced_step_restore (struct displaced_step_inferior_state *displaced,
 static displaced_step_finish_status
 displaced_step_finish (thread_info *event_thread, enum gdb_signal signal)
 {
-  struct displaced_step_inferior_state *displaced
-    = get_displaced_stepping_state (event_thread->inf);
+  displaced_step_thread_state *displaced
+    = get_displaced_stepping_state (event_thread);
 
-  /* Was this event for the thread we displaced?  */
-  if (displaced->step_thread != event_thread)
+  /* Was this thread performing a displaced step?  */
+  if (!displaced->in_progress ())
     return DISPLACED_STEP_FINISH_STATUS_OK;
 
   /* Fixup may need to read memory/registers.  Switch to the thread
@@ -1858,35 +1780,10 @@ displaced_step_finish (thread_info *event_thread, enum gdb_signal signal)
 
   displaced_step_reset_cleanup cleanup (displaced);
 
-  displaced_step_restore (displaced, displaced->step_thread->ptid);
-
-  /* Did the instruction complete successfully?  */
-  if (signal == GDB_SIGNAL_TRAP
-      && !(target_stopped_by_watchpoint ()
-	   && (gdbarch_have_nonsteppable_watchpoint (displaced->step_gdbarch)
-	       || target_have_steppable_watchpoint ())))
-    {
-      /* Fix up the resulting state.  */
-      gdbarch_displaced_step_fixup (displaced->step_gdbarch,
-				    displaced->step_closure.get (),
-				    displaced->step_original,
-				    displaced->step_copy,
-				    get_thread_regcache (displaced->step_thread));
-
-      return DISPLACED_STEP_FINISH_STATUS_OK;
-    }
-  else
-    {
-      /* Since the instruction didn't complete, all we can do is
-	 relocate the PC.  */
-      struct regcache *regcache = get_thread_regcache (event_thread);
-      CORE_ADDR pc = regcache_read_pc (regcache);
-
-      pc = displaced->step_original + (pc - displaced->step_copy);
-      regcache_write_pc (regcache, pc);
-
-      return DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED;
-    }
+  /* Do the fixup, and release the resources acquired to do the displaced
+     step. */
+  return gdbarch_displaced_step_finish (displaced->get_original_gdbarch (),
+					event_thread, signal);
 }
 
 /* Data to be passed around while handling an event.  This data is
@@ -1934,14 +1831,22 @@ static step_over_what thread_still_needs_step_over (struct thread_info *tp);
 static bool
 start_step_over (void)
 {
-  struct thread_info *tp, *next;
+  thread_info *next;
+  bool started = false;
 
   /* Don't start a new step-over if we already have an in-line
      step-over operation ongoing.  */
   if (step_over_info_valid_p ())
-    return false;
+    return started;
+
+  /* Steal the global thread step over chain.  */
+  thread_info *threads_to_step = global_thread_step_over_chain_head;
+  global_thread_step_over_chain_head = NULL;
 
-  for (tp = global_thread_step_over_chain_head; tp != NULL; tp = next)
+  infrun_debug_printf ("stealing global queue of threads to step, length = %d",
+		       thread_step_over_chain_length (threads_to_step));
+
+  for (thread_info *tp = threads_to_step; tp != NULL; tp = next)
     {
       struct execution_control_state ecss;
       struct execution_control_state *ecs = &ecss;
@@ -1950,12 +1855,7 @@ start_step_over (void)
 
       gdb_assert (!tp->stop_requested);
 
-      next = global_thread_step_over_chain_next (tp);
-
-      /* If this inferior already has a displaced step in process,
-	 don't start a new one.  */
-      if (displaced_step_in_progress (tp->inf))
-	continue;
+      next = thread_step_over_chain_next (threads_to_step, tp);
 
       step_what = thread_still_needs_step_over (tp);
       must_be_in_line = ((step_what & STEP_OVER_WATCHPOINT)
@@ -1965,13 +1865,10 @@ start_step_over (void)
       /* We currently stop all threads of all processes to step-over
 	 in-line.  If we need to start a new in-line step-over, let
 	 any pending displaced steps finish first.  */
-      if (must_be_in_line && displaced_step_in_progress_any_inferior ())
-	return false;
-
-      global_thread_step_over_chain_remove (tp);
+      if (must_be_in_line && displaced_step_in_progress_any_thread ())
+	continue;
 
-      if (global_thread_step_over_chain_head == NULL)
-	infrun_debug_printf ("step-over queue now empty");
+      thread_step_over_chain_remove (&threads_to_step, tp);
 
       if (tp->control.trap_expected
 	  || tp->resumed
@@ -2005,13 +1902,27 @@ start_step_over (void)
       if (!ecs->wait_some_more)
 	error (_("Command aborted."));
 
-      gdb_assert (tp->resumed);
+      /* If the thread's step over could not be initiated, it was re-added
+	 to the global step over chain.  */
+      if (tp->resumed)
+	{
+	  infrun_debug_printf ("[%s] was resumed.",
+			       target_pid_to_str (tp->ptid).c_str ());
+	  gdb_assert (!thread_is_in_step_over_chain (tp));
+	}
+      else
+	{
+	  infrun_debug_printf ("[%s] was NOT resumed.",
+			       target_pid_to_str (tp->ptid).c_str ());
+	  gdb_assert (thread_is_in_step_over_chain (tp));
+	}
 
       /* If we started a new in-line step-over, we're done.  */
       if (step_over_info_valid_p ())
 	{
 	  gdb_assert (tp->control.trap_expected);
-	  return true;
+	  started = true;
+	  break;
 	}
 
       if (!target_is_non_stop_p ())
@@ -2024,7 +1935,8 @@ start_step_over (void)
 	  /* With remote targets (at least), in all-stop, we can't
 	     issue any further remote commands until the program stops
 	     again.  */
-	  return true;
+	  started = true;
+	  break;
 	}
 
       /* Either the thread no longer needed a step-over, or a new
@@ -2033,7 +1945,30 @@ start_step_over (void)
 	 displaced step on a thread of other process. */
     }
 
-  return false;
+    /* If there are threads left in the THREADS_TO_STEP list, but we have
+       detected that we can't start anything more, put back these threads
+       in the global list.  */
+    if (threads_to_step == NULL)
+      infrun_debug_printf ("step-over queue now empty");
+    else
+      {
+	infrun_debug_printf ("putting back %d threads to step in global queue",
+			     thread_step_over_chain_length (threads_to_step));
+
+	while (threads_to_step != nullptr)
+	  {
+	    thread_info *thread = threads_to_step;
+
+	    /* Remove from that list.  */
+	    thread_step_over_chain_remove (&threads_to_step, thread);
+
+	    /* Add to global list.  */
+	    global_thread_step_over_chain_enqueue (thread);
+
+	  }
+      }
+
+  return started;
 }
 
 /* Update global variables holding ptids to hold NEW_PTID if they were
@@ -3618,18 +3553,16 @@ prepare_for_detach (void)
   struct inferior *inf = current_inferior ();
   ptid_t pid_ptid = ptid_t (inf->pid);
 
-  displaced_step_inferior_state *displaced = get_displaced_stepping_state (inf);
-
   /* Is any thread of this process displaced stepping?  If not,
      there's nothing else to do.  */
-  if (displaced->step_thread == nullptr)
+  if (displaced_step_in_progress (inf))
     return;
 
   infrun_debug_printf ("displaced-stepping in-process while detaching");
 
   scoped_restore restore_detaching = make_scoped_restore (&inf->detaching, true);
 
-  while (displaced->step_thread != nullptr)
+  while (displaced_step_in_progress (inf))
     {
       struct execution_control_state ecss;
       struct execution_control_state *ecs;
@@ -5293,25 +5226,23 @@ handle_inferior_event (struct execution_control_state *ecs)
       {
 	struct regcache *regcache = get_thread_regcache (ecs->event_thread);
 	struct gdbarch *gdbarch = regcache->arch ();
+	inferior *parent_inf = find_inferior_ptid (ecs->target, ecs->ptid);
+
+	if (ecs->ws.kind == TARGET_WAITKIND_FORKED)
+	  {
+	    /* Restore in the child process any displaced stepping buffers that
+	       were in use at the time of the fork.  */
+	    gdbarch_displaced_step_restore_all_in_ptid
+	      (gdbarch, parent_inf, ecs->ws.value.related_pid);
+	  }
 
 	/* If checking displaced stepping is supported, and thread
 	   ecs->ptid is displaced stepping.  */
 	if (displaced_step_in_progress_thread (ecs->event_thread))
 	  {
-	    struct inferior *parent_inf
-	      = find_inferior_ptid (ecs->target, ecs->ptid);
 	    struct regcache *child_regcache;
 	    CORE_ADDR parent_pc;
 
-	    if (ecs->ws.kind == TARGET_WAITKIND_FORKED)
-	      {
-		struct displaced_step_inferior_state *displaced
-		  = get_displaced_stepping_state (parent_inf);
-
-		/* Restore scratch pad for child process.  */
-		displaced_step_restore (displaced, ecs->ws.value.related_pid);
-	      }
-
 	    /* GDB has got TARGET_WAITKIND_FORKED or TARGET_WAITKIND_VFORKED,
 	       indicating that the displaced stepping of syscall instruction
 	       has been done.  Perform cleanup for parent process here.  Note
diff --git a/gdb/infrun.h b/gdb/infrun.h
index c83cb333083..d5e6d279f1a 100644
--- a/gdb/infrun.h
+++ b/gdb/infrun.h
@@ -226,9 +226,6 @@ extern void clear_exit_convenience_vars (void);
 /* Dump LEN bytes at BUF in hex to a string and return it.  */
 extern std::string displaced_step_dump_bytes (const gdb_byte *buf, size_t len);
 
-extern struct displaced_step_copy_insn_closure *
-  get_displaced_step_copy_insn_closure_by_addr (CORE_ADDR addr);
-
 extern void update_observer_mode (void);
 
 extern void signal_catch_update (const unsigned int *);
diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c
index c57181765e0..f3d1ccb4d1c 100644
--- a/gdb/linux-tdep.c
+++ b/gdb/linux-tdep.c
@@ -199,6 +199,9 @@ struct linux_info
      yet.  Positive if we tried looking it up, and found it.  Negative
      if we tried looking it up but failed.  */
   int vsyscall_range_p = 0;
+
+  /* Inferior's displaced step buffer.  */
+  gdb::optional<displaced_step_buffer> disp_step_buf;
 };
 
 /* Per-inferior data key.  */
@@ -2530,6 +2533,61 @@ linux_displaced_step_location (struct gdbarch *gdbarch)
   return addr;
 }
 
+/* Implementation gdbarch_displaced_step_prepare.  */
+
+displaced_step_prepare_status
+linux_displaced_step_prepare (gdbarch *arch, thread_info *thread,
+			      CORE_ADDR &displaced_pc)
+{
+  linux_info *per_inferior = get_linux_inferior_data (thread->inf);
+
+  if (!per_inferior->disp_step_buf.has_value ())
+    {
+      CORE_ADDR disp_step_buf_addr
+	= linux_displaced_step_location (thread->inf->gdbarch);
+
+      per_inferior->disp_step_buf.emplace (disp_step_buf_addr);
+    }
+
+  return per_inferior->disp_step_buf->prepare (thread, displaced_pc);
+}
+
+/* Implementation gdbarch_displaced_step_finish.  */
+
+displaced_step_finish_status
+linux_displaced_step_finish (gdbarch *arch, thread_info *thread, gdb_signal sig)
+{
+  linux_info *per_inferior = get_linux_inferior_data (thread->inf);
+
+  gdb_assert (per_inferior->disp_step_buf.has_value ());
+
+  return per_inferior->disp_step_buf->finish (arch, thread, sig);
+}
+
+const displaced_step_copy_insn_closure *
+linux_displaced_step_copy_insn_closure_by_addr (inferior *inf, CORE_ADDR addr)
+{
+  linux_info *per_inferior = linux_inferior_data.get (inf);
+
+  if (per_inferior == nullptr
+      || !per_inferior->disp_step_buf.has_value ())
+    return nullptr;
+
+  return per_inferior->disp_step_buf->copy_insn_closure_by_addr (addr);
+}
+
+void
+linux_displaced_step_restore_all_in_ptid (inferior *parent_inf, ptid_t ptid)
+{
+  linux_info *per_inferior = linux_inferior_data.get (parent_inf);
+
+  if (per_inferior == nullptr
+      || !per_inferior->disp_step_buf.has_value ())
+    return;
+
+  per_inferior->disp_step_buf->restore_in_ptid (ptid);
+}
+
 /* See linux-tdep.h.  */
 
 CORE_ADDR
@@ -2578,8 +2636,19 @@ show_dump_excluded_mappings (struct ui_file *file, int from_tty,
    various GNU/Linux architectures and machine types.  */
 
 void
-linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
+linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch,
+		bool supports_displaced_step)
 {
+  if (supports_displaced_step)
+    {
+      set_gdbarch_displaced_step_prepare (gdbarch, linux_displaced_step_prepare);
+      set_gdbarch_displaced_step_finish (gdbarch, linux_displaced_step_finish);
+      set_gdbarch_displaced_step_copy_insn_closure_by_addr
+	(gdbarch, linux_displaced_step_copy_insn_closure_by_addr);
+      set_gdbarch_displaced_step_restore_all_in_ptid
+	(gdbarch, linux_displaced_step_restore_all_in_ptid);
+    }
+
   set_gdbarch_core_pid_to_str (gdbarch, linux_core_pid_to_str);
   set_gdbarch_info_proc (gdbarch, linux_info_proc);
   set_gdbarch_core_info_proc (gdbarch, linux_core_info_proc);
@@ -2609,6 +2678,7 @@ _initialize_linux_tdep ()
   /* Observers used to invalidate the cache when needed.  */
   gdb::observers::inferior_exit.attach (invalidate_linux_cache_inf);
   gdb::observers::inferior_appeared.attach (invalidate_linux_cache_inf);
+  gdb::observers::inferior_execd.attach (invalidate_linux_cache_inf);
 
   add_setshow_boolean_cmd ("use-coredump-filter", class_files,
 			   &use_coredump_filter, _("\
diff --git a/gdb/linux-tdep.h b/gdb/linux-tdep.h
index 91c28738f52..0f83dc3c781 100644
--- a/gdb/linux-tdep.h
+++ b/gdb/linux-tdep.h
@@ -21,7 +21,9 @@
 #define LINUX_TDEP_H
 
 #include "bfd.h"
+#include "displaced-stepping.h"
 
+struct inferior;
 struct regcache;
 
 /* Enum used to define the extra fields of the siginfo type used by an
@@ -57,7 +59,30 @@ extern int linux_gdb_signal_to_target (struct gdbarch *gdbarch,
    the target auxiliary vector.  */
 extern CORE_ADDR linux_displaced_step_location (struct gdbarch *gdbarch);
 
-extern void linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch);
+
+/* Implementation of gdbarch_displaced_step_prepare.  */
+
+extern displaced_step_prepare_status linux_displaced_step_prepare
+  (gdbarch *arch, thread_info *thread, CORE_ADDR &displaced_pc);
+
+/* Implementation of gdbarch_displaced_step_finish.  */
+
+extern displaced_step_finish_status linux_displaced_step_finish
+  (gdbarch *arch, thread_info *thread, gdb_signal sig);
+
+/* Implementation of gdbarch_displaced_step_copy_insn_closure_by_addr.  */
+
+extern const displaced_step_copy_insn_closure *
+  linux_displaced_step_copy_insn_closure_by_addr
+    (inferior *inf, CORE_ADDR addr);
+
+/* Implementation of gdbarch_displaced_step_restore_all_in_ptid.  */
+
+extern void linux_displaced_step_restore_all_in_ptid (inferior *parent_inf,
+						      ptid_t ptid);
+
+extern void linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch,
+			    bool supports_displaced_step);
 
 extern int linux_is_uclinux (void);
 
diff --git a/gdb/m32r-linux-tdep.c b/gdb/m32r-linux-tdep.c
index 60a52e60b6e..961d54a2ca2 100644
--- a/gdb/m32r-linux-tdep.c
+++ b/gdb/m32r-linux-tdep.c
@@ -449,7 +449,7 @@ static void
 m32r_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
 
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   /* Since EVB register is not available for native debug, we reduce
      the number of registers.  */
diff --git a/gdb/m68k-linux-tdep.c b/gdb/m68k-linux-tdep.c
index 63dd53e71d6..509333558ec 100644
--- a/gdb/m68k-linux-tdep.c
+++ b/gdb/m68k-linux-tdep.c
@@ -385,7 +385,7 @@ m68k_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
 
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   tdep->jb_pc = M68K_LINUX_JB_PC;
   tdep->jb_elt_size = M68K_LINUX_JB_ELEMENT_SIZE;
diff --git a/gdb/microblaze-linux-tdep.c b/gdb/microblaze-linux-tdep.c
index be710bedb64..2a91e1bb39a 100644
--- a/gdb/microblaze-linux-tdep.c
+++ b/gdb/microblaze-linux-tdep.c
@@ -117,7 +117,7 @@ static void
 microblaze_linux_init_abi (struct gdbarch_info info,
 			   struct gdbarch *gdbarch)
 {
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   set_gdbarch_memory_remove_breakpoint (gdbarch,
 					microblaze_linux_memory_remove_breakpoint);
diff --git a/gdb/mips-linux-tdep.c b/gdb/mips-linux-tdep.c
index aa2bd53348d..9ca59e5b296 100644
--- a/gdb/mips-linux-tdep.c
+++ b/gdb/mips-linux-tdep.c
@@ -1531,7 +1531,7 @@ mips_linux_init_abi (struct gdbarch_info info,
   enum mips_abi abi = mips_abi (gdbarch);
   struct tdesc_arch_data *tdesc_data = info.tdesc_data;
 
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   /* Get the syscall number from the arch's register.  */
   set_gdbarch_get_syscall_number (gdbarch, mips_linux_get_syscall_number);
diff --git a/gdb/mn10300-linux-tdep.c b/gdb/mn10300-linux-tdep.c
index b5c391c0ccc..27645b1260c 100644
--- a/gdb/mn10300-linux-tdep.c
+++ b/gdb/mn10300-linux-tdep.c
@@ -704,7 +704,7 @@ am33_linux_sigframe_cache_init (const struct tramp_frame *self,
 static void
 am33_linux_init_osabi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   set_gdbarch_iterate_over_regset_sections
     (gdbarch, am33_iterate_over_regset_sections);
diff --git a/gdb/nios2-linux-tdep.c b/gdb/nios2-linux-tdep.c
index e6703ae9bcc..b5c12852c70 100644
--- a/gdb/nios2-linux-tdep.c
+++ b/gdb/nios2-linux-tdep.c
@@ -219,7 +219,7 @@ nios2_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
 
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   /* Shared library handling.  */
   set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
diff --git a/gdb/or1k-linux-tdep.c b/gdb/or1k-linux-tdep.c
index 15677f9cfaf..33ddd10e85f 100644
--- a/gdb/or1k-linux-tdep.c
+++ b/gdb/or1k-linux-tdep.c
@@ -140,7 +140,7 @@ or1k_linux_sigframe_init (const struct tramp_frame *self,
 static void
 or1k_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   set_solib_svr4_fetch_link_map_offsets (gdbarch,
 					 svr4_ilp32_fetch_link_map_offsets);
diff --git a/gdb/ppc-linux-tdep.c b/gdb/ppc-linux-tdep.c
index 8cd8ef893ad..e6c10dd83db 100644
--- a/gdb/ppc-linux-tdep.c
+++ b/gdb/ppc-linux-tdep.c
@@ -1993,7 +1993,7 @@ ppc_linux_init_abi (struct gdbarch_info info,
   static const char *const stap_register_indirection_suffixes[] = { ")",
 								    NULL };
 
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   /* PPC GNU/Linux uses either 64-bit or 128-bit long doubles; where
      128-bit, they can be either IBM long double or IEEE quad long double.
@@ -2143,9 +2143,6 @@ ppc_linux_init_abi (struct gdbarch_info info,
 	}
     }
 
-  set_gdbarch_displaced_step_location (gdbarch,
-				       linux_displaced_step_location);
-
   /* Support reverse debugging.  */
   set_gdbarch_process_record (gdbarch, ppc_process_record);
   set_gdbarch_process_record_signal (gdbarch, ppc_linux_record_signal);
diff --git a/gdb/riscv-linux-tdep.c b/gdb/riscv-linux-tdep.c
index fce838097e2..a2238ad786e 100644
--- a/gdb/riscv-linux-tdep.c
+++ b/gdb/riscv-linux-tdep.c
@@ -159,7 +159,7 @@ riscv_linux_sigframe_init (const struct tramp_frame *self,
 static void
 riscv_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   set_gdbarch_software_single_step (gdbarch, riscv_software_single_step);
 
diff --git a/gdb/rs6000-aix-tdep.c b/gdb/rs6000-aix-tdep.c
index 03e573b436a..b5b398ed4d7 100644
--- a/gdb/rs6000-aix-tdep.c
+++ b/gdb/rs6000-aix-tdep.c
@@ -1132,10 +1132,12 @@ rs6000_aix_init_osabi (struct gdbarch_info info, struct gdbarch *gdbarch)
   set_gdbarch_software_single_step (gdbarch, rs6000_software_single_step);
 
   /* Displaced stepping is currently not supported in combination with
-     software single-stepping.  */
+     software single-stepping.  These override the values set by
+     rs6000_gdbarch_init.  */
   set_gdbarch_displaced_step_copy_insn (gdbarch, NULL);
   set_gdbarch_displaced_step_fixup (gdbarch, NULL);
-  set_gdbarch_displaced_step_location (gdbarch, NULL);
+  set_gdbarch_displaced_step_prepare (gdbarch, NULL);
+  set_gdbarch_displaced_step_finish (gdbarch, NULL);
 
   set_gdbarch_push_dummy_call (gdbarch, rs6000_push_dummy_call);
   set_gdbarch_return_value (gdbarch, rs6000_return_value);
diff --git a/gdb/rs6000-tdep.c b/gdb/rs6000-tdep.c
index 626b47e244f..1a7c7fcb9f1 100644
--- a/gdb/rs6000-tdep.c
+++ b/gdb/rs6000-tdep.c
@@ -153,6 +153,31 @@ static const char *const powerpc_vector_strings[] =
 static enum powerpc_vector_abi powerpc_vector_abi_global = POWERPC_VEC_AUTO;
 static const char *powerpc_vector_abi_string = "auto";
 
+/* PowerPC-related per-inferior data.  */
+
+struct ppc_inferior_data
+{
+  /* This is an optional in case we add more fields to ppc_inferior_data, we
+     don't want it instantiated as soon as we get the ppc_inferior_data for an
+     inferior.  */
+  gdb::optional<displaced_step_buffer> disp_step_buf;
+};
+
+static inferior_key<ppc_inferior_data> ppc_inferior_data_key;
+
+/* Get the per-inferior PowerPC data for INF.  */
+
+static ppc_inferior_data *
+get_ppc_per_inferior (inferior *inf)
+{
+  ppc_inferior_data *per_inf = ppc_inferior_data_key.get (inf);
+
+  if (per_inf == nullptr)
+    per_inf = ppc_inferior_data_key.emplace (inf);
+
+  return per_inf;
+}
+
 /* To be used by skip_prologue.  */
 
 struct rs6000_framedata
@@ -979,6 +1004,53 @@ ppc_displaced_step_fixup (struct gdbarch *gdbarch,
 				    from + offset);
 }
 
+/* Implementation of gdbarch_displaced_step_prepare.  */
+
+static displaced_step_prepare_status
+ppc_displaced_step_prepare  (gdbarch *arch, thread_info *thread,
+			     CORE_ADDR &displaced_pc)
+{
+  ppc_inferior_data *per_inferior = get_ppc_per_inferior (thread->inf);
+
+  if (!per_inferior->disp_step_buf.has_value ())
+    {
+      /* Figure out where the displaced step buffer is.  */
+      CORE_ADDR disp_step_buf_addr
+	= displaced_step_at_entry_point (thread->inf->gdbarch);
+
+      per_inferior->disp_step_buf.emplace (disp_step_buf_addr);
+    }
+
+  return per_inferior->disp_step_buf->prepare (thread, displaced_pc);
+}
+
+/* Implementation of gdbarch_displaced_step_finish.  */
+
+static displaced_step_finish_status
+ppc_displaced_step_finish (gdbarch *arch, thread_info *thread,
+			   gdb_signal sig)
+{
+  ppc_inferior_data *per_inferior = get_ppc_per_inferior (thread->inf);
+
+  gdb_assert (per_inferior->disp_step_buf.has_value ());
+
+  return per_inferior->disp_step_buf->finish (arch, thread, sig);
+}
+
+/* Implementation of gdbarch_displaced_step_restore_all_in_ptid.  */
+
+static void
+ppc_displaced_step_restore_all_in_ptid (inferior *parent_inf, ptid_t ptid)
+{
+  ppc_inferior_data *per_inferior = ppc_inferior_data_key.get (parent_inf);
+
+  if (per_inferior == nullptr
+      || !per_inferior->disp_step_buf.has_value ())
+    return;
+
+  per_inferior->disp_step_buf->restore_in_ptid (ptid);
+}
+
 /* Always use hardware single-stepping to execute the
    displaced instruction.  */
 static bool
@@ -6990,8 +7062,10 @@ rs6000_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
   set_gdbarch_displaced_step_hw_singlestep (gdbarch,
 					    ppc_displaced_step_hw_singlestep);
   set_gdbarch_displaced_step_fixup (gdbarch, ppc_displaced_step_fixup);
-  set_gdbarch_displaced_step_location (gdbarch,
-				       displaced_step_at_entry_point);
+  set_gdbarch_displaced_step_prepare (gdbarch, ppc_displaced_step_prepare);
+  set_gdbarch_displaced_step_finish (gdbarch, ppc_displaced_step_finish);
+  set_gdbarch_displaced_step_restore_all_in_ptid
+    (gdbarch, ppc_displaced_step_restore_all_in_ptid);
 
   set_gdbarch_max_insn_length (gdbarch, PPC_INSN_SIZE);
 
diff --git a/gdb/s390-linux-tdep.c b/gdb/s390-linux-tdep.c
index 8020832d978..14e92d2c6f3 100644
--- a/gdb/s390-linux-tdep.c
+++ b/gdb/s390-linux-tdep.c
@@ -1119,7 +1119,7 @@ s390_linux_init_abi_any (struct gdbarch_info info, struct gdbarch *gdbarch)
 
   tdep->s390_syscall_record = s390_linux_syscall_record;
 
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   /* Register handling.  */
   set_gdbarch_core_read_description (gdbarch, s390_core_read_description);
diff --git a/gdb/s390-tdep.c b/gdb/s390-tdep.c
index f1af2d20c4a..83623ad1e5d 100644
--- a/gdb/s390-tdep.c
+++ b/gdb/s390-tdep.c
@@ -7036,7 +7036,10 @@ s390_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
   set_gdbarch_displaced_step_copy_insn (gdbarch,
 					s390_displaced_step_copy_insn);
   set_gdbarch_displaced_step_fixup (gdbarch, s390_displaced_step_fixup);
-  set_gdbarch_displaced_step_location (gdbarch, linux_displaced_step_location);
+  set_gdbarch_displaced_step_prepare (gdbarch, linux_displaced_step_prepare);
+  set_gdbarch_displaced_step_finish (gdbarch, linux_displaced_step_finish);
+  set_gdbarch_displaced_step_restore_all_in_ptid
+    (gdbarch, linux_displaced_step_restore_all_in_ptid);
   set_gdbarch_displaced_step_hw_singlestep (gdbarch, s390_displaced_step_hw_singlestep);
   set_gdbarch_software_single_step (gdbarch, s390_software_single_step);
   set_gdbarch_max_insn_length (gdbarch, S390_MAX_INSTR_SIZE);
diff --git a/gdb/sh-linux-tdep.c b/gdb/sh-linux-tdep.c
index 91272001561..b7c66b70294 100644
--- a/gdb/sh-linux-tdep.c
+++ b/gdb/sh-linux-tdep.c
@@ -184,7 +184,7 @@ static struct tramp_frame sh_linux_rt_sigreturn_tramp_frame = {
 static void
 sh_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   /* GNU/Linux uses SVR4-style shared libraries.  */
   set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
diff --git a/gdb/sparc-linux-tdep.c b/gdb/sparc-linux-tdep.c
index 693ca6d8e34..3dbc65ca24d 100644
--- a/gdb/sparc-linux-tdep.c
+++ b/gdb/sparc-linux-tdep.c
@@ -422,7 +422,7 @@ sparc32_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
 
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   tdep->gregset = &sparc32_linux_gregset;
   tdep->sizeof_gregset = 152;
diff --git a/gdb/sparc64-linux-tdep.c b/gdb/sparc64-linux-tdep.c
index a7bb64a1673..10a6eb52778 100644
--- a/gdb/sparc64-linux-tdep.c
+++ b/gdb/sparc64-linux-tdep.c
@@ -365,7 +365,7 @@ sparc64_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
 
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   tdep->gregset = &sparc64_linux_gregset;
   tdep->sizeof_gregset = 288;
diff --git a/gdb/testsuite/gdb.arch/amd64-disp-step-avx.exp b/gdb/testsuite/gdb.arch/amd64-disp-step-avx.exp
index 5dd827a40ae..a2cd682d543 100644
--- a/gdb/testsuite/gdb.arch/amd64-disp-step-avx.exp
+++ b/gdb/testsuite/gdb.arch/amd64-disp-step-avx.exp
@@ -97,7 +97,7 @@ proc disp_step_func { func } {
     gdb_test_no_output "set debug displaced on"
 
     gdb_test "continue" \
-	"Continuing.*displaced pc to.*Breakpoint.*, ${test_end_label} ().*" \
+	"Continuing.*prepared successfully .*Breakpoint.*, ${test_end_label} ().*" \
 	"continue to ${test_end_label}"
 
     gdb_test_no_output "set debug displaced off"
diff --git a/gdb/testsuite/gdb.threads/forking-threads-plus-breakpoint.exp b/gdb/testsuite/gdb.threads/forking-threads-plus-breakpoint.exp
index c4c9596be31..4ce30154bdf 100644
--- a/gdb/testsuite/gdb.threads/forking-threads-plus-breakpoint.exp
+++ b/gdb/testsuite/gdb.threads/forking-threads-plus-breakpoint.exp
@@ -48,7 +48,7 @@ proc probe_displaced_stepping_support {} {
 	# that breakpoint.
 	gdb_test_no_output "set debug displaced 1"
 	gdb_test_multiple "next" "probe" {
-	    -re "displaced pc to.*$gdb_prompt $" {
+	    -re "prepared successfully .*$gdb_prompt $" {
 		pass "supported"
 	    }
 	    -re ".*$gdb_prompt $" {
diff --git a/gdb/testsuite/gdb.threads/non-stop-fair-events.exp b/gdb/testsuite/gdb.threads/non-stop-fair-events.exp
index bc1351f688e..11776616150 100644
--- a/gdb/testsuite/gdb.threads/non-stop-fair-events.exp
+++ b/gdb/testsuite/gdb.threads/non-stop-fair-events.exp
@@ -69,7 +69,7 @@ set displaced_stepping_enabled 0
 set msg "check displaced-stepping"
 gdb_test_no_output "set debug displaced 1"
 gdb_test_multiple "next" $msg {
-    -re "displaced pc to.*$gdb_prompt $" {
+    -re "prepared successfully .*$gdb_prompt $" {
 	set displaced_stepping_enabled 1
     }
     -re ".*$gdb_prompt $" {
diff --git a/gdb/thread.c b/gdb/thread.c
index ceb19df1db6..a3c9aed1754 100644
--- a/gdb/thread.c
+++ b/gdb/thread.c
@@ -362,10 +362,10 @@ step_over_chain_enqueue (struct thread_info **list_p, struct thread_info *tp)
     }
 }
 
-/* Remove TP from step-over chain LIST_P.  */
+/* See gdbthread.h.  */
 
-static void
-thread_step_over_chain_remove (struct thread_info **list_p, struct thread_info *tp)
+void
+thread_step_over_chain_remove (thread_info **list_p, thread_info *tp)
 {
   gdb_assert (tp->step_over_next != NULL);
   gdb_assert (tp->step_over_prev != NULL);
@@ -385,12 +385,20 @@ thread_step_over_chain_remove (struct thread_info **list_p, struct thread_info *
 
 /* See gdbthread.h.  */
 
+thread_info *
+thread_step_over_chain_next (thread_info *chain_head, thread_info *tp)
+{
+  thread_info *next = tp->step_over_next;
+
+  return next == chain_head ? NULL : next;
+}
+
+/* See gdbthread.h.  */
+
 struct thread_info *
 global_thread_step_over_chain_next (struct thread_info *tp)
 {
-  struct thread_info *next = tp->step_over_next;
-
-  return (next == global_thread_step_over_chain_head ? NULL : next);
+  return thread_step_over_chain_next (global_thread_step_over_chain_head, tp);
 }
 
 /* See gdbthread.h.  */
@@ -403,9 +411,32 @@ thread_is_in_step_over_chain (struct thread_info *tp)
 
 /* See gdbthread.h.  */
 
+int
+thread_step_over_chain_length (thread_info *tp)
+{
+  if (tp == nullptr)
+    return 0;
+
+  int num = 1;
+  thread_info *iter = tp->step_over_next;
+
+  while (iter != tp)
+    {
+      num++;
+      iter = iter->step_over_next;
+    }
+
+  return num;
+}
+
+/* See gdbthread.h.  */
+
 void
 global_thread_step_over_chain_enqueue (struct thread_info *tp)
 {
+  infrun_debug_printf ("enqueueing thread %s in global step over chain",
+		       target_pid_to_str (tp->ptid).c_str ());
+
   step_over_chain_enqueue (&global_thread_step_over_chain_head, tp);
 }
 
@@ -414,6 +445,9 @@ global_thread_step_over_chain_enqueue (struct thread_info *tp)
 void
 global_thread_step_over_chain_remove (struct thread_info *tp)
 {
+  infrun_debug_printf ("removing thread %s from global step over chain",
+		       target_pid_to_str (tp->ptid).c_str ());
+
   thread_step_over_chain_remove (&global_thread_step_over_chain_head, tp);
 }
 
diff --git a/gdb/tic6x-linux-tdep.c b/gdb/tic6x-linux-tdep.c
index 1b626b5a174..5a8d7c7f825 100644
--- a/gdb/tic6x-linux-tdep.c
+++ b/gdb/tic6x-linux-tdep.c
@@ -167,7 +167,7 @@ tic6x_uclinux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
 
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   /* Shared library handling.  */
   set_solib_ops (gdbarch, &dsbt_so_ops);
diff --git a/gdb/tilegx-linux-tdep.c b/gdb/tilegx-linux-tdep.c
index 18dfe8892d8..14cfafe33de 100644
--- a/gdb/tilegx-linux-tdep.c
+++ b/gdb/tilegx-linux-tdep.c
@@ -111,7 +111,7 @@ tilegx_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
   int arch_size = gdbarch_addr_bit (gdbarch);
 
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   tramp_frame_prepend_unwinder (gdbarch, &tilegx_linux_rt_sigframe);
 
diff --git a/gdb/xtensa-linux-tdep.c b/gdb/xtensa-linux-tdep.c
index b419970ecea..c2aeb8e9397 100644
--- a/gdb/xtensa-linux-tdep.c
+++ b/gdb/xtensa-linux-tdep.c
@@ -110,7 +110,7 @@ xtensa_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
       set_gdbarch_num_pseudo_regs (gdbarch, tdep->num_pseudo_regs);
     }
 
-  linux_init_abi (info, gdbarch);
+  linux_init_abi (info, gdbarch, false);
 
   set_solib_svr4_fetch_link_map_offsets
     (gdbarch, svr4_ilp32_fetch_link_map_offsets);
-- 
2.28.0


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

* [PATCH 10/12] gdb: change linux gdbarch data from post to pre-init
  2020-11-10 21:46 [PATCH 00/12] Concurrent displaced stepping Simon Marchi
                   ` (8 preceding siblings ...)
  2020-11-10 21:46 ` [PATCH 09/12] gdb: move displaced stepping logic to gdbarch, allow starting concurrent displaced steps Simon Marchi
@ 2020-11-10 21:46 ` Simon Marchi
  2020-11-25  1:41   ` Pedro Alves
  2020-11-10 21:46 ` [PATCH 11/12] gdb: make displaced stepping implementation capable of managing multiple buffers Simon Marchi
  2020-11-10 21:46 ` [PATCH 12/12] gdb: use two displaced step buffers on amd64/Linux Simon Marchi
  11 siblings, 1 reply; 44+ messages in thread
From: Simon Marchi @ 2020-11-10 21:46 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

The following patch will need to fill a field in linux_gdbarch_data
while the gdbarch is being built.  linux_gdbarch_data is currently
allocated as a post-init gdbarch data, meaning it's not possible to fill
it before the gdbarch is completely initialized.  Change it to a
pre-init gdbarch data to allow this.

The init_linux_gdbarch_data function doesn't use the created gdbarch,
it only allocates the linux_gdbarch_data structure on the gdbarch's
obstack, so the change is trivial.

gdb/ChangeLog:

	* linux-tdep.c (init_linux_gdbarch_data): Change parameter to
	obkstack.
	(_initialize_linux_tdep): Register pre-init gdb data instead of
	post-init.

Change-Id: If35ce91b6bb5435680d43b9268d811d95661644f
---
 gdb/linux-tdep.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c
index f3d1ccb4d1c..767cca4afd0 100644
--- a/gdb/linux-tdep.c
+++ b/gdb/linux-tdep.c
@@ -171,9 +171,9 @@ struct linux_gdbarch_data
   };
 
 static void *
-init_linux_gdbarch_data (struct gdbarch *gdbarch)
+init_linux_gdbarch_data (struct obstack *obstack)
 {
-  return GDBARCH_OBSTACK_ZALLOC (gdbarch, struct linux_gdbarch_data);
+  return obstack_zalloc<linux_gdbarch_data> (obstack);
 }
 
 static struct linux_gdbarch_data *
@@ -2673,7 +2673,7 @@ void
 _initialize_linux_tdep ()
 {
   linux_gdbarch_data_handle =
-    gdbarch_data_register_post_init (init_linux_gdbarch_data);
+    gdbarch_data_register_pre_init (init_linux_gdbarch_data);
 
   /* Observers used to invalidate the cache when needed.  */
   gdb::observers::inferior_exit.attach (invalidate_linux_cache_inf);
-- 
2.28.0


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

* [PATCH 11/12] gdb: make displaced stepping implementation capable of managing multiple buffers
  2020-11-10 21:46 [PATCH 00/12] Concurrent displaced stepping Simon Marchi
                   ` (9 preceding siblings ...)
  2020-11-10 21:46 ` [PATCH 10/12] gdb: change linux gdbarch data from post to pre-init Simon Marchi
@ 2020-11-10 21:46 ` Simon Marchi
  2020-11-25  1:41   ` Pedro Alves
  2020-11-10 21:46 ` [PATCH 12/12] gdb: use two displaced step buffers on amd64/Linux Simon Marchi
  11 siblings, 1 reply; 44+ messages in thread
From: Simon Marchi @ 2020-11-10 21:46 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

The displaced_step_buffer class, introduced in the previous patch,
manages access to a single displaced step buffer.  Change it into
displaced_step_buffers (note the plural), which manages access to
multiple displaced step buffers.

When preparing a displaced step for a thread, it looks for an unused
buffer.

For now, all users still pass a single displaced step buffer, so no real
behavior change is expected here.  The following patch makes a user pass
more than one buffer, so the functionality introduced by this patch is
going to be useful in the next one.

gdb/ChangeLog:

	* displaced-stepping.h (struct displaced_step_buffer): Rename
	to...
	(struct displaced_step_buffers): ... this.
	<m_addr, m_current_thread, m_copy_insn_closure>: Remove.
	<struct displaced_step_buffer>: New inner class.
	<m_buffers>: New.
	* displaced-stepping.c (displaced_step_buffer::prepare): Rename
	to...
	(displaced_step_buffers::prepare): ... this, adjust for multiple
	buffers.
	(displaced_step_buffer::finish):  Rename to...
	(displaced_step_buffers::finish): ... this, adjust for multiple
	buffers.
	(displaced_step_buffer::copy_insn_closure_by_addr): Rename to...
	(displaced_step_buffers::copy_insn_closure_by_addr): ... this,
	adjust for multiple buffers.
	(displaced_step_buffer::restore_in_ptid): Rename to...
	(displaced_step_buffers::restore_in_ptid): ... this, adjust for
	multiple buffers.
	* linux-tdep.h (linux_init_abi): Change supports_displaced_step
	for num_disp_step_buffers.
	* linux-tdep.c (struct linux_gdbarch_data)
	<num_disp_step_buffers>: New field.
	(struct linux_info) <disp_step_buf>: Rename to...
	<disp_step_bufs>: ... this, change type to
	displaced_step_buffers.
	(linux_displaced_step_prepare): Use
	linux_gdbarch_data::num_disp_step_buffers to create that number
	of buffers.
	(linux_displaced_step_finish): Adjust.
	(linux_displaced_step_copy_insn_closure_by_addr): Adjust.
	(linux_displaced_step_restore_all_in_ptid): Adjust.
	(linux_init_abi): Change supports_displaced_step parameter for
	num_disp_step_buffers, save it in linux_gdbarch_data.
	* aarch64-linux-tdep.c (aarch64_linux_init_abi): Adjust.
	* alpha-linux-tdep.c (alpha_linux_init_abi): Adjust.
	* amd64-linux-tdep.c (amd64_linux_init_abi_common): Change
	supports_displaced_step parameter for num_disp_step_buffers.
	(amd64_linux_init_abi): Adjust.
	(amd64_x32_linux_init_abi): Adjust.
	* arc-linux-tdep.c (arc_linux_init_osabi): Adjust.
	* arm-linux-tdep.c (arm_linux_init_abi): Adjust.
	* bfin-linux-tdep.c (bfin_linux_init_abi): Adjust.
	* cris-linux-tdep.c (cris_linux_init_abi): Adjust.
	* csky-linux-tdep.c (csky_linux_init_abi): Adjust.
	* frv-linux-tdep.c (frv_linux_init_abi): Adjust.
	* hppa-linux-tdep.c (hppa_linux_init_abi): Adjust.
	* i386-linux-tdep.c (i386_linux_init_abi): Adjust.
	* ia64-linux-tdep.c (ia64_linux_init_abi): Adjust.
	* m32r-linux-tdep.c (m32r_linux_init_abi): Adjust.
	* m68k-linux-tdep.c (m68k_linux_init_abi):
	* microblaze-linux-tdep.c (microblaze_linux_init_abi):
	* mips-linux-tdep.c (mips_linux_init_abi): Adjust.
	* mn10300-linux-tdep.c (am33_linux_init_osabi): Adjust.
	* nios2-linux-tdep.c (nios2_linux_init_abi): Adjust.
	* or1k-linux-tdep.c (or1k_linux_init_abi): Adjust.
	* ppc-linux-tdep.c (ppc_linux_init_abi): Adjust.
	* riscv-linux-tdep.c (riscv_linux_init_abi): Adjust.
	* rs6000-tdep.c (struct ppc_inferior_data) <disp_step_buf>:
	Change type to displaced_step_buffers.
	* s390-linux-tdep.c (s390_linux_init_abi_any): Adjust.
	* sh-linux-tdep.c (sh_linux_init_abi): Adjust.
	* sparc-linux-tdep.c (sparc32_linux_init_abi): Adjust.
	* sparc64-linux-tdep.c (sparc64_linux_init_abi): Adjust.
	* tic6x-linux-tdep.c (tic6x_uclinux_init_abi): Adjust.
	* tilegx-linux-tdep.c (tilegx_linux_init_abi): Adjust.
	* xtensa-linux-tdep.c (xtensa_linux_init_abi): Adjust.

Change-Id: Ia9c02f207da2c9e1d9188020139619122392bb70
---
 gdb/aarch64-linux-tdep.c    |   2 +-
 gdb/alpha-linux-tdep.c      |   2 +-
 gdb/amd64-linux-tdep.c      |   8 +-
 gdb/arc-linux-tdep.c        |   2 +-
 gdb/arm-linux-tdep.c        |   2 +-
 gdb/bfin-linux-tdep.c       |   2 +-
 gdb/cris-linux-tdep.c       |   2 +-
 gdb/csky-linux-tdep.c       |   2 +-
 gdb/displaced-stepping.c    | 171 +++++++++++++++++++++++-------------
 gdb/displaced-stepping.h    |  46 +++++++---
 gdb/frv-linux-tdep.c        |   2 +-
 gdb/hppa-linux-tdep.c       |   2 +-
 gdb/i386-linux-tdep.c       |   2 +-
 gdb/ia64-linux-tdep.c       |   2 +-
 gdb/linux-tdep.c            |  54 ++++++++----
 gdb/linux-tdep.h            |   2 +-
 gdb/m32r-linux-tdep.c       |   2 +-
 gdb/m68k-linux-tdep.c       |   2 +-
 gdb/microblaze-linux-tdep.c |   2 +-
 gdb/mips-linux-tdep.c       |   2 +-
 gdb/mn10300-linux-tdep.c    |   2 +-
 gdb/nios2-linux-tdep.c      |   2 +-
 gdb/or1k-linux-tdep.c       |   2 +-
 gdb/ppc-linux-tdep.c        |   2 +-
 gdb/riscv-linux-tdep.c      |   2 +-
 gdb/rs6000-tdep.c           |   2 +-
 gdb/s390-linux-tdep.c       |   2 +-
 gdb/sh-linux-tdep.c         |   2 +-
 gdb/sparc-linux-tdep.c      |   2 +-
 gdb/sparc64-linux-tdep.c    |   2 +-
 gdb/tic6x-linux-tdep.c      |   2 +-
 gdb/tilegx-linux-tdep.c     |   2 +-
 gdb/xtensa-linux-tdep.c     |   2 +-
 33 files changed, 214 insertions(+), 123 deletions(-)

diff --git a/gdb/aarch64-linux-tdep.c b/gdb/aarch64-linux-tdep.c
index 4fe7babe59c..ce697ff246e 100644
--- a/gdb/aarch64-linux-tdep.c
+++ b/gdb/aarch64-linux-tdep.c
@@ -1445,7 +1445,7 @@ aarch64_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 
   tdep->lowest_pc = 0x8000;
 
-  linux_init_abi (info, gdbarch, true);
+  linux_init_abi (info, gdbarch, 1);
 
   set_solib_svr4_fetch_link_map_offsets (gdbarch,
 					 svr4_lp64_fetch_link_map_offsets);
diff --git a/gdb/alpha-linux-tdep.c b/gdb/alpha-linux-tdep.c
index a6d6b15e9fd..520dd980d88 100644
--- a/gdb/alpha-linux-tdep.c
+++ b/gdb/alpha-linux-tdep.c
@@ -356,7 +356,7 @@ alpha_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
   struct gdbarch_tdep *tdep;
 
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   /* Hook into the DWARF CFI frame unwinder.  */
   alpha_dwarf2_init_abi (info, gdbarch);
diff --git a/gdb/amd64-linux-tdep.c b/gdb/amd64-linux-tdep.c
index a81bb9039df..60707ed7aaf 100644
--- a/gdb/amd64-linux-tdep.c
+++ b/gdb/amd64-linux-tdep.c
@@ -1796,11 +1796,11 @@ amd64_dtrace_parse_probe_argument (struct gdbarch *gdbarch,
 
 static void
 amd64_linux_init_abi_common(struct gdbarch_info info, struct gdbarch *gdbarch,
-			    bool supports_displaced_step)
+			    int num_disp_step_buffers)
 {
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
 
-  linux_init_abi (info, gdbarch, supports_displaced_step);
+  linux_init_abi (info, gdbarch, num_disp_step_buffers);
 
   tdep->sigtramp_p = amd64_linux_sigtramp_p;
   tdep->sigcontext_addr = amd64_linux_sigcontext_addr;
@@ -1880,7 +1880,7 @@ amd64_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
   if (!valid_p)
     return;
 
-  amd64_linux_init_abi_common (info, gdbarch, true);
+  amd64_linux_init_abi_common (info, gdbarch, 1);
 
   /* Initialize the amd64_linux_record_tdep.  */
   /* These values are the size of the type that will be used in a system
@@ -2095,7 +2095,7 @@ amd64_x32_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
   if (!valid_p)
     return;
 
-  amd64_linux_init_abi_common (info, gdbarch, false);
+  amd64_linux_init_abi_common (info, gdbarch, 0);
 
   /* Initialize the amd64_x32_linux_record_tdep.  */
   /* These values are the size of the type that will be used in a system
diff --git a/gdb/arc-linux-tdep.c b/gdb/arc-linux-tdep.c
index 71dcdcc1811..5ad66bd5eec 100644
--- a/gdb/arc-linux-tdep.c
+++ b/gdb/arc-linux-tdep.c
@@ -434,7 +434,7 @@ arc_linux_init_osabi (struct gdbarch_info info, struct gdbarch *gdbarch)
    */
   tdep->jb_pc = 15;
 
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   /* Set up target dependent GDB architecture entries.  */
   set_gdbarch_cannot_fetch_register (gdbarch, arc_linux_cannot_fetch_register);
diff --git a/gdb/arm-linux-tdep.c b/gdb/arm-linux-tdep.c
index d164cff3dff..11e71e56b46 100644
--- a/gdb/arm-linux-tdep.c
+++ b/gdb/arm-linux-tdep.c
@@ -1721,7 +1721,7 @@ arm_linux_init_abi (struct gdbarch_info info,
 								    NULL };
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
 
-  linux_init_abi (info, gdbarch, true);
+  linux_init_abi (info, gdbarch, 1);
 
   tdep->lowest_pc = 0x8000;
   if (info.byte_order_for_code == BFD_ENDIAN_BIG)
diff --git a/gdb/bfin-linux-tdep.c b/gdb/bfin-linux-tdep.c
index fc2f1d9ac65..bc2bb1aad45 100644
--- a/gdb/bfin-linux-tdep.c
+++ b/gdb/bfin-linux-tdep.c
@@ -150,7 +150,7 @@ bfin_linux_get_syscall_number (struct gdbarch *gdbarch,
 static void
 bfin_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   /* Set the sigtramp frame sniffer.  */
   tramp_frame_prepend_unwinder (gdbarch, &bfin_linux_sigframe);
diff --git a/gdb/cris-linux-tdep.c b/gdb/cris-linux-tdep.c
index 85cbf4cc093..4ae1cdd1390 100644
--- a/gdb/cris-linux-tdep.c
+++ b/gdb/cris-linux-tdep.c
@@ -35,7 +35,7 @@ cris_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
 
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   if (tdep->cris_version == 32)
     /* Threaded debugging is only supported on CRISv32 for now.  */
diff --git a/gdb/csky-linux-tdep.c b/gdb/csky-linux-tdep.c
index 184fa5ffb23..a0d32b5f2d0 100644
--- a/gdb/csky-linux-tdep.c
+++ b/gdb/csky-linux-tdep.c
@@ -233,7 +233,7 @@ csky_linux_rt_sigreturn_tramp_frame = {
 static void
 csky_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   /* Shared library handling.  */
   set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
diff --git a/gdb/displaced-stepping.c b/gdb/displaced-stepping.c
index f92eb52293f..1d7c6d8ae4d 100644
--- a/gdb/displaced-stepping.c
+++ b/gdb/displaced-stepping.c
@@ -44,86 +44,114 @@ show_debug_displaced (struct ui_file *file, int from_tty,
 }
 
 displaced_step_prepare_status
-displaced_step_buffer::prepare (thread_info *thread, CORE_ADDR &displaced_pc)
+displaced_step_buffers::prepare (thread_info *thread, CORE_ADDR &displaced_pc)
 {
   gdb_assert (!thread->displaced_step_state.in_progress ());
 
-  /* Is a thread currently using the buffer?  */
-  if (m_current_thread != nullptr)
-    {
-      /* If so, it better not be this thread.  */
-      gdb_assert (thread != m_current_thread);
-      return DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE;
-    }
+  /* Sanity check: the thread should not be using a buffer at this point.  */
+  for (displaced_step_buffer &buf : m_buffers)
+    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_max_insn_length (arch);
 
-  if (breakpoint_in_range_p (aspace, m_addr, len))
-    {
-      /* There's a breakpoint set in the scratch pad location range
-	 (which is usually around the entry point).  We'd either
-	 install it before resuming, which would overwrite/corrupt the
-	 scratch pad, or if it was already inserted, this displaced
-	 step would overwrite it.  The latter is OK in the sense that
-	 we already assume that no thread is going to execute the code
-	 in the scratch pad range (after initial startup) anyway, but
-	 the former is unacceptable.  Simply punt and fallback to
-	 stepping over this breakpoint in-line.  */
-      displaced_debug_printf ("breakpoint set in scratch pad.  "
-			      "Stepping over breakpoint in-line instead.");
+  /* Search for an unused buffer.  */
+  displaced_step_buffer *buffer = nullptr;
+  displaced_step_prepare_status fail_status
+    = DISPLACED_STEP_PREPARE_STATUS_ERROR;
 
-      return DISPLACED_STEP_PREPARE_STATUS_ERROR;
+  for (displaced_step_buffer &candidate : m_buffers)
+    {
+      bool bp_in_range = breakpoint_in_range_p (aspace, candidate.addr, len);
+      bool is_free = candidate.current_thread == nullptr;
+
+      if (!bp_in_range)
+        {
+          if (is_free)
+	    {
+	      buffer = &candidate;
+	      break;
+	    }
+	  else
+	    {
+	      /* This buffer would be suitable, but it's used right now.  */
+	      fail_status = DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE;
+	    }
+        }
+      else
+	{
+	  /* There's a breakpoint set in the scratch pad location range
+	     (which is usually around the entry point).  We'd either
+	     install it before resuming, which would overwrite/corrupt the
+	     scratch pad, or if it was already inserted, this displaced
+	     step would overwrite it.  The latter is OK in the sense that
+	     we already assume that no thread is going to execute the code
+	     in the scratch pad range (after initial startup) anyway, but
+	     the former is unacceptable.  Simply punt and fallback to
+	     stepping over this breakpoint in-line.  */
+	  displaced_debug_printf ("breakpoint set in displaced stepping "
+				  "buffer at %s, can't use.",
+				  paddress (arch, candidate.addr));
+	}
     }
 
-  m_original_pc = regcache_read_pc (regcache);
-  displaced_pc = m_addr;
+  if (buffer == nullptr)
+    return fail_status;
+
+  displaced_debug_printf ("selected buffer at %s",
+			  paddress (arch, buffer->addr));
+
+  /* Save the original PC of the thread.  */
+  buffer->original_pc = regcache_read_pc (regcache);
+
+  /* Return displaced step buffer address to caller.  */
+  displaced_pc = buffer->addr;
 
   /* Save the original contents of the displaced stepping buffer.  */
-  m_saved_copy.resize (len);
+  buffer->saved_copy.resize (len);
 
-  int status = target_read_memory (m_addr, m_saved_copy.data (), len);
+  int status = target_read_memory (buffer->addr,
+				    buffer->saved_copy.data (), len);
   if (status != 0)
     throw_error (MEMORY_ERROR,
 		 _("Error accessing memory address %s (%s) for "
 		   "displaced-stepping scratch space."),
-		 paddress (arch, m_addr), safe_strerror (status));
+		 paddress (arch, buffer->addr), safe_strerror (status));
 
   displaced_debug_printf ("saved %s: %s",
-			  paddress (arch, m_addr),
+			  paddress (arch, buffer->addr),
 			  displaced_step_dump_bytes
-			    (m_saved_copy.data (), len).c_str ());
+			  (buffer->saved_copy.data (), len).c_str ());
 
-  m_copy_insn_closure = gdbarch_displaced_step_copy_insn (arch,
-							  m_original_pc,
-							  m_addr,
-							  regcache);
-  if (m_copy_insn_closure == nullptr)
+  buffer->copy_insn_closure
+    = gdbarch_displaced_step_copy_insn (arch, buffer->original_pc,
+					buffer->addr, regcache);
+  if (buffer->copy_insn_closure == nullptr)
     {
       /* The architecture doesn't know how or want to displaced step
-        this instruction or instruction sequence.  Fallback to
-        stepping over the breakpoint in-line.  */
+	 this instruction or instruction sequence.  Fallback to
+	 stepping over the breakpoint in-line.  */
       return DISPLACED_STEP_PREPARE_STATUS_ERROR;
     }
 
   try
     {
       /* Resume execution at the copy.  */
-      regcache_write_pc (regcache, m_addr);
+      regcache_write_pc (regcache, buffer->addr);
     }
   catch (...)
     {
       /* Failed to write the PC.  Release the architecture's displaced
-         stepping resources and the thread's displaced stepping state.  */
-      m_copy_insn_closure.reset ();
+	 stepping resources and the thread's displaced stepping state.  */
+      buffer->copy_insn_closure.reset ();
 
       return DISPLACED_STEP_PREPARE_STATUS_ERROR;
     }
 
   /* This marks the buffer as being in use.  */
-  m_current_thread = thread;
+  buffer->current_thread = thread;
 
   return DISPLACED_STEP_PREPARE_STATUS_OK;
 }
@@ -156,20 +184,34 @@ displaced_step_instruction_executed_successfully (gdbarch *arch,
 }
 
 displaced_step_finish_status
-displaced_step_buffer::finish (gdbarch *arch, thread_info *thread,
-			       gdb_signal sig)
+displaced_step_buffers::finish (gdbarch *arch, thread_info *thread,
+				gdb_signal sig)
 {
   gdb_assert (thread->displaced_step_state.in_progress ());
-  gdb_assert (thread == m_current_thread);
+
+  /* Find the buffer this thread was using.  */
+  displaced_step_buffer *buffer = nullptr;
+
+  for (displaced_step_buffer &candidate : m_buffers)
+    {
+      if (thread == candidate.current_thread)
+	{
+	  buffer = &candidate;
+	  break;
+	}
+    }
+
+  gdb_assert (buffer != nullptr);
 
   ULONGEST len = gdbarch_max_insn_length (arch);
 
-  write_memory_ptid (thread->ptid, m_addr,
-		     m_saved_copy.data (), len);
+  /* Restore memory of the buffer.  */
+  write_memory_ptid (thread->ptid, buffer->addr,
+		     buffer->saved_copy.data (), len);
 
   displaced_debug_printf ("restored %s %s",
 			  target_pid_to_str (thread->ptid).c_str (),
-			  paddress (arch, m_addr));
+			  paddress (arch, buffer->addr));
 
   regcache *rc = get_thread_regcache (thread);
 
@@ -179,8 +221,9 @@ displaced_step_buffer::finish (gdbarch *arch, thread_info *thread,
 
   if (instruction_executed_successfully)
     {
-      gdbarch_displaced_step_fixup (arch, m_copy_insn_closure.get (), m_original_pc,
-				    m_addr, rc);
+      gdbarch_displaced_step_fixup (arch, buffer->copy_insn_closure.get (),
+				    buffer->original_pc,
+				    buffer->addr, rc);
       status = DISPLACED_STEP_FINISH_STATUS_OK;
     }
   else
@@ -188,40 +231,46 @@ displaced_step_buffer::finish (gdbarch *arch, thread_info *thread,
       /* Since the instruction didn't complete, all we can do is relocate the
 	 PC.  */
       CORE_ADDR pc = regcache_read_pc (rc);
-      pc = m_original_pc + (pc - m_addr);
+      pc = buffer->original_pc + (pc - buffer->addr);
       regcache_write_pc (rc, pc);
       status = DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED;
     }
 
-  m_copy_insn_closure.reset ();
-  m_current_thread = nullptr;
+  buffer->copy_insn_closure.reset ();
+  buffer->current_thread = nullptr;
 
   return status;
 }
 
 const displaced_step_copy_insn_closure *
-displaced_step_buffer::copy_insn_closure_by_addr (CORE_ADDR addr)
+displaced_step_buffers::copy_insn_closure_by_addr (CORE_ADDR addr)
 {
-  if (addr == m_addr)
-    return m_copy_insn_closure.get ();
-  else
-    return nullptr;
+  for (const displaced_step_buffer &buffer : m_buffers)
+    {
+      if (addr == buffer.addr)
+	return buffer.copy_insn_closure.get ();
+    }
+
+  return nullptr;
 }
 
 void
-displaced_step_buffer::restore_in_ptid (ptid_t ptid)
+displaced_step_buffers::restore_in_ptid (ptid_t ptid)
 {
-  if (m_current_thread != nullptr)
+  for (const displaced_step_buffer &buffer : m_buffers)
     {
-      regcache *regcache = get_thread_regcache (m_current_thread);
+      if (buffer.current_thread == nullptr)
+	continue;
+
+      regcache *regcache = get_thread_regcache (buffer.current_thread);
       gdbarch *arch = regcache->arch ();
       ULONGEST len = gdbarch_max_insn_length (arch);
 
-      write_memory_ptid (ptid, m_addr, m_saved_copy.data (), len);
+      write_memory_ptid (ptid, buffer.addr, buffer.saved_copy.data (), len);
 
       displaced_debug_printf ("restored in ptid %s %s",
 			      target_pid_to_str (ptid).c_str (),
-			      paddress (arch, m_addr));
+			      paddress (arch, buffer.addr));
     }
 }
 
diff --git a/gdb/displaced-stepping.h b/gdb/displaced-stepping.h
index 6c1da46777c..ce9abcd2307 100644
--- a/gdb/displaced-stepping.h
+++ b/gdb/displaced-stepping.h
@@ -20,6 +20,7 @@
 #ifndef DISPLACED_STEPPING_H
 #define DISPLACED_STEPPING_H
 
+#include "gdbsupport/array-view.h"
 #include "gdbsupport/byte-vector.h"
 
 struct gdbarch;
@@ -142,13 +143,17 @@ struct displaced_step_thread_state
   gdbarch *m_original_gdbarch = nullptr;
 };
 
-/* Manage access to a single displaced stepping buffer.  */
+/* Control access to multiple displaced stepping buffers at fixed addresses.  */
 
-struct displaced_step_buffer
+struct displaced_step_buffers
 {
-  displaced_step_buffer (CORE_ADDR buffer_addr)
-    : m_addr (buffer_addr)
-  {}
+  displaced_step_buffers (gdb::array_view<CORE_ADDR> buffer_addrs)
+  {
+    gdb_assert (buffer_addrs.size () > 0);
+
+    for (CORE_ADDR buffer_addr : buffer_addrs)
+      m_buffers.emplace_back (buffer_addr);
+  }
 
   displaced_step_prepare_status prepare (thread_info *thread,
 					 CORE_ADDR &displaced_pc);
@@ -163,14 +168,33 @@ struct displaced_step_buffer
 
 private:
 
-  CORE_ADDR m_original_pc = 0;
-  const CORE_ADDR m_addr;
+  /* State of a single buffer.  */
+
+  struct displaced_step_buffer
+  {
+    displaced_step_buffer (CORE_ADDR addr)
+      : addr (addr)
+    {}
+
+    const CORE_ADDR addr;
+
+    /* The original PC of the instruction currently begin stepped.  */
+    CORE_ADDR original_pc = 0;
+
+    /* If set, the thread currently using the buffer.  If unset, the buffer is not
+       used.  */
+    thread_info *current_thread = nullptr;
+
+    /* Saved copy of the bytes in the displaced buffer, to be restored once the
+       buffer is no longer used.  */
+    gdb::byte_vector saved_copy;
 
-  /* If set, the thread currently using the buffer.  */
-  thread_info *m_current_thread = nullptr;
+    /* Closure obtained from gdbarch_displaced_step_copy_insn, to be passed to
+       gdbarch_displaced_step_fixup_insn.  */
+    displaced_step_copy_insn_closure_up copy_insn_closure;
+  };
 
-  gdb::byte_vector m_saved_copy;
-  displaced_step_copy_insn_closure_up m_copy_insn_closure;
+  std::vector<displaced_step_buffer> m_buffers;
 };
 
 #endif /* DISPLACED_STEPPING_H */
diff --git a/gdb/frv-linux-tdep.c b/gdb/frv-linux-tdep.c
index c5ae4212ab4..c5b20737654 100644
--- a/gdb/frv-linux-tdep.c
+++ b/gdb/frv-linux-tdep.c
@@ -456,7 +456,7 @@ frv_linux_iterate_over_regset_sections (struct gdbarch *gdbarch,
 static void
 frv_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   /* Set the sigtramp frame sniffer.  */
   frame_unwind_append_unwinder (gdbarch, &frv_linux_sigtramp_frame_unwind); 
diff --git a/gdb/hppa-linux-tdep.c b/gdb/hppa-linux-tdep.c
index a171e582e59..ce85f327d1e 100644
--- a/gdb/hppa-linux-tdep.c
+++ b/gdb/hppa-linux-tdep.c
@@ -489,7 +489,7 @@ hppa_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
 
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   /* GNU/Linux is always ELF.  */
   tdep->is_elf = 1;
diff --git a/gdb/i386-linux-tdep.c b/gdb/i386-linux-tdep.c
index 1b209fd3eff..90ee30969aa 100644
--- a/gdb/i386-linux-tdep.c
+++ b/gdb/i386-linux-tdep.c
@@ -832,7 +832,7 @@ i386_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 
   gdb_assert (tdesc_data);
 
-  linux_init_abi (info, gdbarch, true);
+  linux_init_abi (info, gdbarch, 1);
 
   /* GNU/Linux uses ELF.  */
   i386_elf_init_abi (info, gdbarch);
diff --git a/gdb/ia64-linux-tdep.c b/gdb/ia64-linux-tdep.c
index d6d581ab3dd..3a69f758c11 100644
--- a/gdb/ia64-linux-tdep.c
+++ b/gdb/ia64-linux-tdep.c
@@ -223,7 +223,7 @@ ia64_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
   static const char *const stap_register_indirection_suffixes[] = { "]",
 								    NULL };
 
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   /* Set the method of obtaining the sigcontext addresses at which
      registers are saved.  */
diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c
index 767cca4afd0..a3af21790ff 100644
--- a/gdb/linux-tdep.c
+++ b/gdb/linux-tdep.c
@@ -166,9 +166,10 @@ enum
 static struct gdbarch_data *linux_gdbarch_data_handle;
 
 struct linux_gdbarch_data
-  {
-    struct type *siginfo_type;
-  };
+{
+  struct type *siginfo_type;
+  int num_disp_step_buffers;
+};
 
 static void *
 init_linux_gdbarch_data (struct obstack *obstack)
@@ -200,8 +201,8 @@ struct linux_info
      if we tried looking it up but failed.  */
   int vsyscall_range_p = 0;
 
-  /* Inferior's displaced step buffer.  */
-  gdb::optional<displaced_step_buffer> disp_step_buf;
+  /* Inferior's displaced step buffers.  */
+  gdb::optional<displaced_step_buffers> disp_step_bufs;
 };
 
 /* Per-inferior data key.  */
@@ -2541,15 +2542,25 @@ linux_displaced_step_prepare (gdbarch *arch, thread_info *thread,
 {
   linux_info *per_inferior = get_linux_inferior_data (thread->inf);
 
-  if (!per_inferior->disp_step_buf.has_value ())
+  if (!per_inferior->disp_step_bufs.has_value ())
     {
+      /* Figure out the location of the buffers.  They are contiguous, starting
+	 at DISP_STEP_BUF_ADDR.  They are all of size BUF_LEN.  */
       CORE_ADDR disp_step_buf_addr
 	= linux_displaced_step_location (thread->inf->gdbarch);
+      int buf_len = gdbarch_max_insn_length (arch);
 
-      per_inferior->disp_step_buf.emplace (disp_step_buf_addr);
+      linux_gdbarch_data *gdbarch_data = get_linux_gdbarch_data (arch);
+      gdb_assert (gdbarch_data->num_disp_step_buffers > 0);
+
+      std::vector<CORE_ADDR> buffers;
+      for (int i = 0; i < gdbarch_data->num_disp_step_buffers; i++)
+	buffers.push_back (disp_step_buf_addr + i * buf_len);
+
+      per_inferior->disp_step_bufs.emplace (buffers);
     }
 
-  return per_inferior->disp_step_buf->prepare (thread, displaced_pc);
+  return per_inferior->disp_step_bufs->prepare (thread, displaced_pc);
 }
 
 /* Implementation gdbarch_displaced_step_finish.  */
@@ -2559,9 +2570,9 @@ linux_displaced_step_finish (gdbarch *arch, thread_info *thread, gdb_signal sig)
 {
   linux_info *per_inferior = get_linux_inferior_data (thread->inf);
 
-  gdb_assert (per_inferior->disp_step_buf.has_value ());
+  gdb_assert (per_inferior->disp_step_bufs.has_value ());
 
-  return per_inferior->disp_step_buf->finish (arch, thread, sig);
+  return per_inferior->disp_step_bufs->finish (arch, thread, sig);
 }
 
 const displaced_step_copy_insn_closure *
@@ -2570,10 +2581,10 @@ linux_displaced_step_copy_insn_closure_by_addr (inferior *inf, CORE_ADDR addr)
   linux_info *per_inferior = linux_inferior_data.get (inf);
 
   if (per_inferior == nullptr
-      || !per_inferior->disp_step_buf.has_value ())
+      || !per_inferior->disp_step_bufs.has_value ())
     return nullptr;
 
-  return per_inferior->disp_step_buf->copy_insn_closure_by_addr (addr);
+  return per_inferior->disp_step_bufs->copy_insn_closure_by_addr (addr);
 }
 
 void
@@ -2582,10 +2593,10 @@ linux_displaced_step_restore_all_in_ptid (inferior *parent_inf, ptid_t ptid)
   linux_info *per_inferior = linux_inferior_data.get (parent_inf);
 
   if (per_inferior == nullptr
-      || !per_inferior->disp_step_buf.has_value ())
+      || !per_inferior->disp_step_bufs.has_value ())
     return;
 
-  per_inferior->disp_step_buf->restore_in_ptid (ptid);
+  per_inferior->disp_step_bufs->restore_in_ptid (ptid);
 }
 
 /* See linux-tdep.h.  */
@@ -2633,15 +2644,22 @@ show_dump_excluded_mappings (struct ui_file *file, int from_tty,
 }
 
 /* To be called from the various GDB_OSABI_LINUX handlers for the
-   various GNU/Linux architectures and machine types.  */
+   various GNU/Linux architectures and machine types.
+
+   NUM_DISP_STEP_BUFFERS is the number of displaced step buffers to use.  If 0,
+   displaced stepping is not supported. */
 
 void
 linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch,
-		bool supports_displaced_step)
+		int num_disp_step_buffers)
 {
-  if (supports_displaced_step)
+  if (num_disp_step_buffers > 0)
     {
-      set_gdbarch_displaced_step_prepare (gdbarch, linux_displaced_step_prepare);
+      linux_gdbarch_data *gdbarch_data = get_linux_gdbarch_data (gdbarch);
+      gdbarch_data->num_disp_step_buffers = num_disp_step_buffers;
+
+      set_gdbarch_displaced_step_prepare (gdbarch,
+					  linux_displaced_step_prepare);
       set_gdbarch_displaced_step_finish (gdbarch, linux_displaced_step_finish);
       set_gdbarch_displaced_step_copy_insn_closure_by_addr
 	(gdbarch, linux_displaced_step_copy_insn_closure_by_addr);
diff --git a/gdb/linux-tdep.h b/gdb/linux-tdep.h
index 0f83dc3c781..723eec3dc10 100644
--- a/gdb/linux-tdep.h
+++ b/gdb/linux-tdep.h
@@ -82,7 +82,7 @@ extern void linux_displaced_step_restore_all_in_ptid (inferior *parent_inf,
 						      ptid_t ptid);
 
 extern void linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch,
-			    bool supports_displaced_step);
+			    int num_disp_step_buffers);
 
 extern int linux_is_uclinux (void);
 
diff --git a/gdb/m32r-linux-tdep.c b/gdb/m32r-linux-tdep.c
index 961d54a2ca2..0a1ff780d8c 100644
--- a/gdb/m32r-linux-tdep.c
+++ b/gdb/m32r-linux-tdep.c
@@ -449,7 +449,7 @@ static void
 m32r_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
 
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   /* Since EVB register is not available for native debug, we reduce
      the number of registers.  */
diff --git a/gdb/m68k-linux-tdep.c b/gdb/m68k-linux-tdep.c
index 509333558ec..f057915965f 100644
--- a/gdb/m68k-linux-tdep.c
+++ b/gdb/m68k-linux-tdep.c
@@ -385,7 +385,7 @@ m68k_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
 
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   tdep->jb_pc = M68K_LINUX_JB_PC;
   tdep->jb_elt_size = M68K_LINUX_JB_ELEMENT_SIZE;
diff --git a/gdb/microblaze-linux-tdep.c b/gdb/microblaze-linux-tdep.c
index 2a91e1bb39a..54f89ec0377 100644
--- a/gdb/microblaze-linux-tdep.c
+++ b/gdb/microblaze-linux-tdep.c
@@ -117,7 +117,7 @@ static void
 microblaze_linux_init_abi (struct gdbarch_info info,
 			   struct gdbarch *gdbarch)
 {
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   set_gdbarch_memory_remove_breakpoint (gdbarch,
 					microblaze_linux_memory_remove_breakpoint);
diff --git a/gdb/mips-linux-tdep.c b/gdb/mips-linux-tdep.c
index 9ca59e5b296..ed95c220084 100644
--- a/gdb/mips-linux-tdep.c
+++ b/gdb/mips-linux-tdep.c
@@ -1531,7 +1531,7 @@ mips_linux_init_abi (struct gdbarch_info info,
   enum mips_abi abi = mips_abi (gdbarch);
   struct tdesc_arch_data *tdesc_data = info.tdesc_data;
 
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   /* Get the syscall number from the arch's register.  */
   set_gdbarch_get_syscall_number (gdbarch, mips_linux_get_syscall_number);
diff --git a/gdb/mn10300-linux-tdep.c b/gdb/mn10300-linux-tdep.c
index 27645b1260c..f7586bf3eae 100644
--- a/gdb/mn10300-linux-tdep.c
+++ b/gdb/mn10300-linux-tdep.c
@@ -704,7 +704,7 @@ am33_linux_sigframe_cache_init (const struct tramp_frame *self,
 static void
 am33_linux_init_osabi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   set_gdbarch_iterate_over_regset_sections
     (gdbarch, am33_iterate_over_regset_sections);
diff --git a/gdb/nios2-linux-tdep.c b/gdb/nios2-linux-tdep.c
index b5c12852c70..0a28c9cca6d 100644
--- a/gdb/nios2-linux-tdep.c
+++ b/gdb/nios2-linux-tdep.c
@@ -219,7 +219,7 @@ nios2_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
 
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   /* Shared library handling.  */
   set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
diff --git a/gdb/or1k-linux-tdep.c b/gdb/or1k-linux-tdep.c
index 33ddd10e85f..2779fd0d1e6 100644
--- a/gdb/or1k-linux-tdep.c
+++ b/gdb/or1k-linux-tdep.c
@@ -140,7 +140,7 @@ or1k_linux_sigframe_init (const struct tramp_frame *self,
 static void
 or1k_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   set_solib_svr4_fetch_link_map_offsets (gdbarch,
 					 svr4_ilp32_fetch_link_map_offsets);
diff --git a/gdb/ppc-linux-tdep.c b/gdb/ppc-linux-tdep.c
index e6c10dd83db..57bdd2d7a72 100644
--- a/gdb/ppc-linux-tdep.c
+++ b/gdb/ppc-linux-tdep.c
@@ -1993,7 +1993,7 @@ ppc_linux_init_abi (struct gdbarch_info info,
   static const char *const stap_register_indirection_suffixes[] = { ")",
 								    NULL };
 
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   /* PPC GNU/Linux uses either 64-bit or 128-bit long doubles; where
      128-bit, they can be either IBM long double or IEEE quad long double.
diff --git a/gdb/riscv-linux-tdep.c b/gdb/riscv-linux-tdep.c
index a2238ad786e..623c7d9382c 100644
--- a/gdb/riscv-linux-tdep.c
+++ b/gdb/riscv-linux-tdep.c
@@ -159,7 +159,7 @@ riscv_linux_sigframe_init (const struct tramp_frame *self,
 static void
 riscv_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   set_gdbarch_software_single_step (gdbarch, riscv_software_single_step);
 
diff --git a/gdb/rs6000-tdep.c b/gdb/rs6000-tdep.c
index 1a7c7fcb9f1..235abd873f7 100644
--- a/gdb/rs6000-tdep.c
+++ b/gdb/rs6000-tdep.c
@@ -160,7 +160,7 @@ struct ppc_inferior_data
   /* This is an optional in case we add more fields to ppc_inferior_data, we
      don't want it instantiated as soon as we get the ppc_inferior_data for an
      inferior.  */
-  gdb::optional<displaced_step_buffer> disp_step_buf;
+  gdb::optional<displaced_step_buffers> disp_step_buf;
 };
 
 static inferior_key<ppc_inferior_data> ppc_inferior_data_key;
diff --git a/gdb/s390-linux-tdep.c b/gdb/s390-linux-tdep.c
index 14e92d2c6f3..8588d046bd6 100644
--- a/gdb/s390-linux-tdep.c
+++ b/gdb/s390-linux-tdep.c
@@ -1119,7 +1119,7 @@ s390_linux_init_abi_any (struct gdbarch_info info, struct gdbarch *gdbarch)
 
   tdep->s390_syscall_record = s390_linux_syscall_record;
 
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   /* Register handling.  */
   set_gdbarch_core_read_description (gdbarch, s390_core_read_description);
diff --git a/gdb/sh-linux-tdep.c b/gdb/sh-linux-tdep.c
index b7c66b70294..84bcd300069 100644
--- a/gdb/sh-linux-tdep.c
+++ b/gdb/sh-linux-tdep.c
@@ -184,7 +184,7 @@ static struct tramp_frame sh_linux_rt_sigreturn_tramp_frame = {
 static void
 sh_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   /* GNU/Linux uses SVR4-style shared libraries.  */
   set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
diff --git a/gdb/sparc-linux-tdep.c b/gdb/sparc-linux-tdep.c
index 3dbc65ca24d..71759e19fa8 100644
--- a/gdb/sparc-linux-tdep.c
+++ b/gdb/sparc-linux-tdep.c
@@ -422,7 +422,7 @@ sparc32_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
 
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   tdep->gregset = &sparc32_linux_gregset;
   tdep->sizeof_gregset = 152;
diff --git a/gdb/sparc64-linux-tdep.c b/gdb/sparc64-linux-tdep.c
index 10a6eb52778..fe1b276bc4e 100644
--- a/gdb/sparc64-linux-tdep.c
+++ b/gdb/sparc64-linux-tdep.c
@@ -365,7 +365,7 @@ sparc64_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
 
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   tdep->gregset = &sparc64_linux_gregset;
   tdep->sizeof_gregset = 288;
diff --git a/gdb/tic6x-linux-tdep.c b/gdb/tic6x-linux-tdep.c
index 5a8d7c7f825..7820e2da5a0 100644
--- a/gdb/tic6x-linux-tdep.c
+++ b/gdb/tic6x-linux-tdep.c
@@ -167,7 +167,7 @@ tic6x_uclinux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
 
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   /* Shared library handling.  */
   set_solib_ops (gdbarch, &dsbt_so_ops);
diff --git a/gdb/tilegx-linux-tdep.c b/gdb/tilegx-linux-tdep.c
index 14cfafe33de..37c0790a190 100644
--- a/gdb/tilegx-linux-tdep.c
+++ b/gdb/tilegx-linux-tdep.c
@@ -111,7 +111,7 @@ tilegx_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
   int arch_size = gdbarch_addr_bit (gdbarch);
 
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   tramp_frame_prepend_unwinder (gdbarch, &tilegx_linux_rt_sigframe);
 
diff --git a/gdb/xtensa-linux-tdep.c b/gdb/xtensa-linux-tdep.c
index c2aeb8e9397..fccac7d49fc 100644
--- a/gdb/xtensa-linux-tdep.c
+++ b/gdb/xtensa-linux-tdep.c
@@ -110,7 +110,7 @@ xtensa_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
       set_gdbarch_num_pseudo_regs (gdbarch, tdep->num_pseudo_regs);
     }
 
-  linux_init_abi (info, gdbarch, false);
+  linux_init_abi (info, gdbarch, 0);
 
   set_solib_svr4_fetch_link_map_offsets
     (gdbarch, svr4_ilp32_fetch_link_map_offsets);
-- 
2.28.0


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

* [PATCH 12/12] gdb: use two displaced step buffers on amd64/Linux
  2020-11-10 21:46 [PATCH 00/12] Concurrent displaced stepping Simon Marchi
                   ` (10 preceding siblings ...)
  2020-11-10 21:46 ` [PATCH 11/12] gdb: make displaced stepping implementation capable of managing multiple buffers Simon Marchi
@ 2020-11-10 21:46 ` Simon Marchi
  2020-11-25  1:42   ` Pedro Alves
  11 siblings, 1 reply; 44+ messages in thread
From: Simon Marchi @ 2020-11-10 21:46 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

As observed on a binary compiled on AMD64 Ubuntu 20.04, against glibc
2.31 (I think it's the libc that provides this startup code, right?),
there are enough bytes at the executable's entry point to hold more than
one displaced step buffer.  gdbarch_max_insn_length is 16, and the
code at _start looks like:

0000000000001040 <_start>:
    1040:       f3 0f 1e fa             endbr64
    1044:       31 ed                   xor    %ebp,%ebp
    1046:       49 89 d1                mov    %rdx,%r9
    1049:       5e                      pop    %rsi
    104a:       48 89 e2                mov    %rsp,%rdx
    104d:       48 83 e4 f0             and    $0xfffffffffffffff0,%rsp
    1051:       50                      push   %rax
    1052:       54                      push   %rsp
    1053:       4c 8d 05 56 01 00 00    lea    0x156(%rip),%r8        # 11b0 <__libc_csu_fini>
    105a:       48 8d 0d df 00 00 00    lea    0xdf(%rip),%rcx        # 1140 <__libc_csu_init>
    1061:       48 8d 3d c1 00 00 00    lea    0xc1(%rip),%rdi        # 1129 <main>
    1068:       ff 15 72 2f 00 00       callq  *0x2f72(%rip)        # 3fe0 <__libc_start_main@GLIBC_2.2.5>
    106e:       f4                      hlt
    106f:       90                      nop

The two buffers would occupy [0x1040, 0x1060).

I checked on Alpine, which uses the musl C library, the startup code
looks like:

0000000000001048 <_start>:
    1048:       48 31 ed                xor    %rbp,%rbp
    104b:       48 89 e7                mov    %rsp,%rdi
    104e:       48 8d 35 e3 2d 00 00    lea    0x2de3(%rip),%rsi        # 3e38 <_DYNAMIC>
    1055:       48 83 e4 f0             and    $0xfffffffffffffff0,%rsp
    1059:       e8 00 00 00 00          callq  105e <_start_c>

000000000000105e <_start_c>:
    105e:       48 8b 37                mov    (%rdi),%rsi
    1061:       48 8d 57 08             lea    0x8(%rdi),%rdx
    1065:       45 31 c9                xor    %r9d,%r9d
    1068:       4c 8d 05 47 01 00 00    lea    0x147(%rip),%r8        # 11b6 <_fini>
    106f:       48 8d 0d 8a ff ff ff    lea    -0x76(%rip),%rcx        # 1000 <_init>
    1076:       48 8d 3d 0c 01 00 00    lea    0x10c(%rip),%rdi        # 1189 <main>
    107d:       e9 9e ff ff ff          jmpq   1020 <__libc_start_main@plt>

Even though there's a _start_c symbol, it all appears to be code that
runs once at the very beginning of the program, so it looks fine if the
two buffers occupy [0x1048, 0x1068).

One important thing I discovered while doing this is that when debugging
a dynamically-linked executable, breakpoints in the shared library
loader are hit before executing the _start code, and these breakpoints
may be displaced-stepped.  So it's very important that the buffer bytes
are restored properly after doing the displaced steps, otherwise the
_start code will be corrupted once we try to execute it.

Another thing that made me think about is that library constructors (as
in `__attribute__((constructor))`) run before _start.  And they are free
to spawn threads.  What if one of these threads executes a displaced
step, therefore changing the bytes at _start, while the main thread
executes _start?  That doesn't sound good and I don't know how we could
prevent it.  But this is a problem that predates the current patch.

Even when stress-testing the implementation, by making many threads do
displaced steps over and over, I didn't see a significant performance (I
confirmed that the two buffers were used by checking the "set debug
displaced" logs though).  However, this patch mostly helps make the
feature testable by anybody with an AMD64/Linux machine, so I think it's
useful.

gdb/ChangeLog:

        * amd64-linux-tdep.c (amd64_linux_init_abi): Pass 2 as the
	number of displaced step buffers.

Change-Id: Ia0c96ea0fcda893f4726df6fdac7be5214620112
---
 gdb/amd64-linux-tdep.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/gdb/amd64-linux-tdep.c b/gdb/amd64-linux-tdep.c
index 60707ed7aaf..4f2d83f0286 100644
--- a/gdb/amd64-linux-tdep.c
+++ b/gdb/amd64-linux-tdep.c
@@ -1880,7 +1880,7 @@ amd64_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
   if (!valid_p)
     return;
 
-  amd64_linux_init_abi_common (info, gdbarch, 1);
+  amd64_linux_init_abi_common (info, gdbarch, 2);
 
   /* Initialize the amd64_linux_record_tdep.  */
   /* These values are the size of the type that will be used in a system
-- 
2.28.0


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

* Re: [PATCH 06/12] gdb: introduce status enum for displaced step prepare/finish
  2020-11-10 21:46 ` [PATCH 06/12] gdb: introduce status enum for displaced step prepare/finish Simon Marchi
@ 2020-11-11 23:36   ` Andrew Burgess
  2020-11-25 13:17     ` Simon Marchi
  2020-11-25  1:30   ` Pedro Alves
  1 sibling, 1 reply; 44+ messages in thread
From: Andrew Burgess @ 2020-11-11 23:36 UTC (permalink / raw)
  To: Simon Marchi; +Cc: gdb-patches

I looked though all the patches up to and including this one.  I don't
have too much knowledge of the displaced stepping mechanism, but so
far everything seemed good.

Thanks,
Andrew

* Simon Marchi via Gdb-patches <gdb-patches@sourceware.org> [2020-11-10 16:46:08 -0500]:

> This is a preparatory patch to reduce the size of the diff of the
> upcoming main patch.  It introduces enum types for the return values of
> displaced step "prepare" and "finish" operations.  I find that this
> expresses better the intention of the code, rather than returning
> arbitrary integer values (-1, 0 and 1) which are difficult to remember.
> That makes the code easier to read.
> 
> I put the new enum types in a new displaced-stepping.h file, because I
> introduce that file in a later patch anyway.  Putting it there avoids
> having to move it later.
> 
> There is one change in behavior for displaced_step_finish: it currently
> returns 0 if the thread wasn't doing a displaced step and 1 if the
> thread was doing a displaced step which was executed successfully.  It
> turns out that this distinction is not needed by any caller, so I've
> merged these two cases into "_OK", rather than adding an extra
> enumerator.
> 
> gdb/ChangeLog:
> 
> 	* infrun.c (displaced_step_prepare_throw): Change return type to
> 	displaced_step_prepare_status.
> 	(displaced_step_prepare): Likewise.
> 	(displaced_step_finish): Change return type to
> 	displaced_step_finish_status.
> 	(resume_1): Adjust.
> 	(stop_all_threads): Adjust.
> 	* displaced-stepping.h: New file.
> 
> Change-Id: I5c8fe07212cd398d5b486b5936d9d0807acd3788
> ---
>  gdb/displaced-stepping.h | 46 +++++++++++++++++++++++++
>  gdb/infrun.c             | 72 +++++++++++++++++++++++-----------------
>  2 files changed, 88 insertions(+), 30 deletions(-)
>  create mode 100644 gdb/displaced-stepping.h
> 
> diff --git a/gdb/displaced-stepping.h b/gdb/displaced-stepping.h
> new file mode 100644
> index 00000000000..9c7e85c3769
> --- /dev/null
> +++ b/gdb/displaced-stepping.h
> @@ -0,0 +1,46 @@
> +/* Displaced stepping related things.
> +
> +   Copyright (C) 2020 Free Software Foundation, Inc.
> +
> +   This file is part of GDB.
> +
> +   This program is free software; you can redistribute it and/or modify
> +   it under the terms of the GNU General Public License as published by
> +   the Free Software Foundation; either version 3 of the License, or
> +   (at your option) any later version.
> +
> +   This program is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +   GNU General Public License for more details.
> +
> +   You should have received a copy of the GNU General Public License
> +   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
> +
> +#ifndef DISPLACED_STEPPING_H
> +#define DISPLACED_STEPPING_H
> +
> +enum displaced_step_prepare_status
> +{
> +  /* A displaced stepping buffer was successfully allocated and prepared.  */
> +  DISPLACED_STEP_PREPARE_STATUS_OK,
> +
> +  /* Something bad happened.  */
> +  DISPLACED_STEP_PREPARE_STATUS_ERROR,
> +
> +  /* Not enough resources are available at this time, try again later.  */
> +  DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE,
> +};
> +
> +enum displaced_step_finish_status
> +{
> +  /* Either the instruction was stepped and fixed up, or the specified thread
> +     wasn't executing a displaced step (in which case there's nothing to
> +     finish). */
> +  DISPLACED_STEP_FINISH_STATUS_OK,
> +
> +  /* The thread was executing a displaced step, but didn't complete it.  */
> +  DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED,
> +};
> +
> +#endif /* DISPLACED_STEPPING_H */
> diff --git a/gdb/infrun.c b/gdb/infrun.c
> index cdf198bb307..ed54c4331d7 100644
> --- a/gdb/infrun.c
> +++ b/gdb/infrun.c
> @@ -19,6 +19,7 @@
>     along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
>  
>  #include "defs.h"
> +#include "displaced-stepping.h"
>  #include "infrun.h"
>  #include <ctype.h>
>  #include "symtab.h"
> @@ -1652,11 +1653,13 @@ displaced_step_dump_bytes (const gdb_byte *buf, size_t len)
>     Comments in the code for 'random signals' in handle_inferior_event
>     explain how we handle this case instead.
>  
> -   Returns 1 if preparing was successful -- this thread is going to be
> -   stepped now; 0 if displaced stepping this thread got queued; or -1
> -   if this instruction can't be displaced stepped.  */
> +   Returns DISPLACED_STEP_PREPARE_STATUS_OK if preparing was successful -- this
> +   thread is going to be stepped now; DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE
> +   if displaced stepping this thread got queued; or
> +   DISPLACED_STEP_PREPARE_STATUS_ERROR if this instruction can't be displaced
> +   stepped.  */
>  
> -static int
> +static displaced_step_prepare_status
>  displaced_step_prepare_throw (thread_info *tp)
>  {
>    regcache *regcache = get_thread_regcache (tp);
> @@ -1694,7 +1697,7 @@ displaced_step_prepare_throw (thread_info *tp)
>  			      target_pid_to_str (tp->ptid).c_str ());
>  
>        global_thread_step_over_chain_enqueue (tp);
> -      return 0;
> +      return DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE;
>      }
>    else
>      displaced_debug_printf ("stepping %s now",
> @@ -1725,7 +1728,7 @@ displaced_step_prepare_throw (thread_info *tp)
>        displaced_debug_printf ("breakpoint set in scratch pad.  "
>  			      "Stepping over breakpoint in-line instead.");
>  
> -      return -1;
> +      return DISPLACED_STEP_PREPARE_STATUS_ERROR;
>      }
>  
>    /* Save the original contents of the copy area.  */
> @@ -1749,7 +1752,7 @@ displaced_step_prepare_throw (thread_info *tp)
>        /* The architecture doesn't know how or want to displaced step
>  	 this instruction or instruction sequence.  Fallback to
>  	 stepping over the breakpoint in-line.  */
> -      return -1;
> +      return DISPLACED_STEP_PREPARE_STATUS_ERROR;
>      }
>  
>    /* Save the information we need to fix things up if the step
> @@ -1770,20 +1773,21 @@ displaced_step_prepare_throw (thread_info *tp)
>  
>    displaced_debug_printf ("displaced pc to %s", paddress (gdbarch, copy));
>  
> -  return 1;
> +  return DISPLACED_STEP_PREPARE_STATUS_OK;
>  }
>  
>  /* Wrapper for displaced_step_prepare_throw that disabled further
>     attempts at displaced stepping if we get a memory error.  */
>  
> -static int
> +static displaced_step_prepare_status
>  displaced_step_prepare (thread_info *thread)
>  {
> -  int prepared = -1;
> +  displaced_step_prepare_status status
> +    = DISPLACED_STEP_PREPARE_STATUS_ERROR;
>  
>    try
>      {
> -      prepared = displaced_step_prepare_throw (thread);
> +      status = displaced_step_prepare_throw (thread);
>      }
>    catch (const gdb_exception_error &ex)
>      {
> @@ -1810,7 +1814,7 @@ displaced_step_prepare (thread_info *thread)
>        displaced_state->failed_before = 1;
>      }
>  
> -  return prepared;
> +  return status;
>  }
>  
>  static void
> @@ -1840,22 +1844,24 @@ displaced_step_restore (struct displaced_step_inferior_state *displaced,
>  				    displaced->step_copy));
>  }
>  
> -/* If we displaced stepped an instruction successfully, adjust
> -   registers and memory to yield the same effect the instruction would
> -   have had if we had executed it at its original address, and return
> -   1.  If the instruction didn't complete, relocate the PC and return
> -   -1.  If the thread wasn't displaced stepping, return 0.  */
> +/* If we displaced stepped an instruction successfully, adjust registers and
> +   memory to yield the same effect the instruction would have had if we had
> +   executed it at its original address, and return
> +   DISPLACED_STEP_PREPARE_STATUS_OK.  If the instruction didn't complete,
> +   relocate the PC and return DISPLACED_STEP_PREPARE_STATUS_NOT_EXECUTED.
>  
> -static int
> +   If the thread wasn't displaced stepping, return
> +   DISPLACED_STEP_PREPARE_STATUS_OK as well..  */
> +
> +static displaced_step_finish_status
>  displaced_step_finish (thread_info *event_thread, enum gdb_signal signal)
>  {
>    struct displaced_step_inferior_state *displaced
>      = get_displaced_stepping_state (event_thread->inf);
> -  int ret;
>  
>    /* Was this event for the thread we displaced?  */
>    if (displaced->step_thread != event_thread)
> -    return 0;
> +    return DISPLACED_STEP_FINISH_STATUS_OK;
>  
>    /* Fixup may need to read memory/registers.  Switch to the thread
>       that we're fixing up.  Also, target_stopped_by_watchpoint checks
> @@ -1879,7 +1885,8 @@ displaced_step_finish (thread_info *event_thread, enum gdb_signal signal)
>  				    displaced->step_original,
>  				    displaced->step_copy,
>  				    get_thread_regcache (displaced->step_thread));
> -      ret = 1;
> +
> +      return DISPLACED_STEP_FINISH_STATUS_OK;
>      }
>    else
>      {
> @@ -1890,10 +1897,9 @@ displaced_step_finish (thread_info *event_thread, enum gdb_signal signal)
>  
>        pc = displaced->step_original + (pc - displaced->step_copy);
>        regcache_write_pc (regcache, pc);
> -      ret = -1;
> -    }
>  
> -  return ret;
> +      return DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED;
> +    }
>  }
>  
>  /* Data to be passed around while handling an event.  This data is
> @@ -2417,16 +2423,17 @@ resume_1 (enum gdb_signal sig)
>        && sig == GDB_SIGNAL_0
>        && !current_inferior ()->waiting_for_vfork_done)
>      {
> -      int prepared = displaced_step_prepare (tp);
> +      displaced_step_prepare_status prepare_status
> +	= displaced_step_prepare (tp);
>  
> -      if (prepared == 0)
> +      if (prepare_status == DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE)
>  	{
>  	  infrun_debug_printf ("Got placed in step-over queue");
>  
>  	  tp->control.trap_expected = 0;
>  	  return;
>  	}
> -      else if (prepared < 0)
> +      else if (prepare_status == DISPLACED_STEP_PREPARE_STATUS_ERROR)
>  	{
>  	  /* Fallback to stepping over the breakpoint in-line.  */
>  
> @@ -2440,7 +2447,7 @@ resume_1 (enum gdb_signal sig)
>  
>  	  insert_breakpoints ();
>  	}
> -      else if (prepared > 0)
> +      else if (prepare_status == DISPLACED_STEP_PREPARE_STATUS_OK)
>  	{
>  	  /* Update pc to reflect the new address from which we will
>  	     execute instructions due to displaced stepping.  */
> @@ -2448,6 +2455,9 @@ resume_1 (enum gdb_signal sig)
>  
>  	  step = gdbarch_displaced_step_hw_singlestep (gdbarch);
>  	}
> +      else
> +	gdb_assert_not_reached (_("Invalid displaced_step_prepare_status "
> +				  "value."));
>      }
>  
>    /* Do we need to do it the hard way, w/temp breakpoints?  */
> @@ -4822,7 +4832,8 @@ stop_all_threads (void)
>  		      t->suspend.waitstatus.kind = TARGET_WAITKIND_IGNORE;
>  		      t->suspend.waitstatus_pending_p = 0;
>  
> -		      if (displaced_step_finish (t, GDB_SIGNAL_0) < 0)
> +		      if (displaced_step_finish (t, GDB_SIGNAL_0)
> +			  == DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED)
>  			{
>  			  /* Add it back to the step-over queue.  */
>  			  infrun_debug_printf
> @@ -4850,7 +4861,8 @@ stop_all_threads (void)
>  		      sig = (event.ws.kind == TARGET_WAITKIND_STOPPED
>  			     ? event.ws.value.sig : GDB_SIGNAL_0);
>  
> -		      if (displaced_step_finish (t, sig) < 0)
> +		      if (displaced_step_finish (t, sig)
> +			  == DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED)
>  			{
>  			  /* Add it back to the step-over queue.  */
>  			  t->control.trap_expected = 0;
> -- 
> 2.28.0
> 

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

* Re: [PATCH 01/12] gdb: add inferior_execd observable
  2020-11-10 21:46 ` [PATCH 01/12] gdb: add inferior_execd observable Simon Marchi
@ 2020-11-25  1:28   ` Pedro Alves
  0 siblings, 0 replies; 44+ messages in thread
From: Pedro Alves @ 2020-11-25  1:28 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches

On 11/10/20 9:46 PM, Simon Marchi via Gdb-patches wrote:
> I want to add another action (clearing displaced stepping state) that
> happens when an inferior execs.  I think it would be cleaner to have an
> observer for this event, rather than have infrun know about each other
> sub-component.
> 
> Replace the calls to solib_create_inferior_hook and
> jit_inferior_created_hook in follow_exec by observers.
> 
> gdb/ChangeLog:
> 
> 	* observable.h (inferior_execd): Declare new observable.
> 	* observable.c (inferior_execd): Declare new observable.
> 	* infrun.c (follow_exec): Notify inferior_execd observer.
> 	* jit.c (jit_inferior_created_hook): Make static.
> 	(_initialize_jit): Register inferior_execd observer.
> 	* jit.h (jit_inferior_created_hook): Remove declaration.
> 	* solib.c (_initialize_solib): Register inferior_execd observer.

OK.

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

* Re: [PATCH 02/12] gdb: clear inferior displaced stepping state on exec
  2020-11-10 21:46 ` [PATCH 02/12] gdb: clear inferior displaced stepping state on exec Simon Marchi
@ 2020-11-25  1:28   ` Pedro Alves
  2020-12-01  4:27     ` Simon Marchi
  0 siblings, 1 reply; 44+ messages in thread
From: Pedro Alves @ 2020-11-25  1:28 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches

On 11/10/20 9:46 PM, Simon Marchi via Gdb-patches wrote:
> When a process does an exec, all its program space is replaced with the
> newly loaded executable.  All non-main threads disappear and the main
> thread starts executing at the entry point of the new executable.
> 
> Things can go wrong if a displaced step operation is in progress while
> we process the exec event.
> 
> If the main thread is the one executing the displaced step: when that
> thread (now executing in the new executable) stops somewhere (say, at a
> breakpoint), displaced_step_fixup will run and clear up the state.  We
> will execute the "fixup" phase for the instruction we single-stepped in
> the old program space.  We are now in a completely different context,
> so doing the fixup may corrupt the state.
> 
> If it is a non-main thread that is doing the displaced step: while
> handling the exec event, GDB deletes the thread_info representing that
> thread (since the thread doesn't exist in the inferior after the exec).
> But inferior::displaced_step_state::step_thread will still point to it.
> When handling events later, this condition, in displaced_step_fixup,
> will likely never be true:
> 
>     /* Was this event for the thread we displaced?  */
>     if (displaced->step_thread != event_thread)
>       return 0;
> 
> ... since displaced->step_thread points to a deleted thread (unless that
> storage gets re-used for a new thread_info, but that wouldn't be good
> either).  This effectively makes the displaced stepping buffer occupied
> for ever.  When a thread in the new program space will want to do a
> displaced step, it will wait for ever.
> 
> I think we simply need to reset the displaced stepping state of the
> inferior on exec.  Everything execution-related that existed before the
> exec is now gone.
> 
> I tried to write a test where a non-main thread displaced-steps an exec
> syscall, where things would hang due to the displaced step buffer not
> getting released.  However, due to PR 26754 [1], it is hard to make it
> stable.  So I'm not including a test for this patch.  If you have an
> idea for another way to test this without triggering this bug, I'd like
> to hear it.
> 
> [1] https://sourceware.org/bugzilla/show_bug.cgi?id=26754

I can't think of another way to test this.

> 
> gdb/ChangeLog:
> 
> 	* infrun.c (infrun_inferior_execd): New function.
> 	(_initialize_infrun): Attach inferior_execd observer.

OK.

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

* Re: [PATCH 03/12] gdb: rename things related to step over chains
  2020-11-10 21:46 ` [PATCH 03/12] gdb: rename things related to step over chains Simon Marchi
@ 2020-11-25  1:28   ` Pedro Alves
  2020-11-25 13:16     ` Simon Marchi
  0 siblings, 1 reply; 44+ messages in thread
From: Pedro Alves @ 2020-11-25  1:28 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches

On 11/10/20 9:46 PM, Simon Marchi via Gdb-patches wrote:
> Rename step_over_queue_head to global_thread_step_over_chain_head, to
> make it more obvious when reading code that we are touching the global
> queue.  Rename all functions that operate on it to have "global" in
> their name, to make it clear on which chain they operate on.  Also, in a
> subsequent patch, we'll need both global and non-global versions of
> these functions, so it will be easier to do the distinction if they are
> named properly.
> 
> Normalize the naming to use "chain" everywhere instead of sometimes
> "queue", sometimes "chain".
> 
> I also reworded a few comments in gdbthread.h.  They implied that the
> step over chain is per-inferior, when in reality there is only one
> global chain, not one per inferior, as far as I understand.
> 
> gdb/ChangeLog:
> 
>         * gdbthread.h (thread_step_over_chain_enqueue): Rename to...
>         (global_thread_step_over_chain_enqueue): ... this.  Update all
> 	users.
>         (thread_step_over_chain_remove): Rename to...
>         (global_thread_step_over_chain_remove): ... this.  Update all
> 	users.
>         (thread_step_over_chain_next): Rename to...
>         (global_thread_step_over_chain_next): ... this.  Update all
> 	users.
>         * infrun.h (step_over_queue_head): Rename to...
> 	(global_thread_step_over_chain_head): ... this.  Update all
> 	users.
>         * infrun.c (step_over_queue_head): Rename to...
> 	(global_thread_step_over_chain_head): ... this.  Update all
> 	users.
>         * thread.c (step_over_chain_remove): Rename to...
> 	(thread_step_over_chain_remove): ... this.  Update all users.
> 	(thread_step_over_chain_next): Rename to...
>         (global_thread_step_over_chain_next): ... this.  Update all
> 	users.
>         (thread_step_over_chain_enqueue): Rename to...
>         (global_thread_step_over_chain_enqueue): ... this.  Update all
> 	users.
>         (thread_step_over_chain_remove): Rename to...
>         (global_thread_step_over_chain_remove): ... this.  Update all
> 	users.
> 
> Change-Id: Iabbf57d83c01321ca199d83fadb57f5b04e4d6d9
> ---
>  gdb/gdbthread.h | 16 ++++++++--------
>  gdb/infrun.c    | 26 +++++++++++++-------------
>  gdb/infrun.h    |  4 ++--
>  gdb/thread.c    | 18 +++++++++---------
>  4 files changed, 32 insertions(+), 32 deletions(-)
> 
> diff --git a/gdb/gdbthread.h b/gdb/gdbthread.h
> index 630727e2fb5..b5a32087289 100644
> --- a/gdb/gdbthread.h
> +++ b/gdb/gdbthread.h
> @@ -741,20 +741,20 @@ extern value *get_last_thread_stack_temporary (struct thread_info *tp);
>  extern bool value_in_thread_stack_temporaries (struct value *,
>  					       struct thread_info *thr);
>  
> -/* Add TP to the end of its inferior's pending step-over chain.  */
> +/* Add TP to the end of the global pending step-over chain.  */
>  
> -extern void thread_step_over_chain_enqueue (struct thread_info *tp);
> +extern void global_thread_step_over_chain_enqueue (struct thread_info *tp);
>  
> -/* Remove TP from its inferior's pending step-over chain.  */
> +/* Remove TP from the global pending step-over chain.  */
>  
> -extern void thread_step_over_chain_remove (struct thread_info *tp);
> +extern void global_thread_step_over_chain_remove (struct thread_info *tp);
>  
> -/* Return the next thread in the step-over chain starting at TP.  NULL
> -   if TP is the last entry in the chain.  */
> +/* Return the thread following TP in the global step-over chain, or NULL if TP
> +   is the last entry in the chain.  */
>  
> -extern struct thread_info *thread_step_over_chain_next (struct thread_info *tp);
> +extern struct thread_info *global_thread_step_over_chain_next (struct thread_info *tp);

Line too long now.

> --- a/gdb/thread.c
> +++ b/gdb/thread.c
> @@ -207,7 +207,7 @@ set_thread_exited (thread_info *tp, bool silent)
>  {
>    /* Dead threads don't need to step-over.  Remove from queue.  */

"Remove from chain", I guess.

>    if (tp->step_over_next != NULL)
> -    thread_step_over_chain_remove (tp);
> +    global_thread_step_over_chain_remove (tp);
>  

Otherwise OK.

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

* Re: [PATCH 04/12] gdb: rename displaced_step_closure to displaced_step_copy_insn_closure
  2020-11-10 21:46 ` [PATCH 04/12] gdb: rename displaced_step_closure to displaced_step_copy_insn_closure Simon Marchi
@ 2020-11-25  1:29   ` Pedro Alves
  0 siblings, 0 replies; 44+ messages in thread
From: Pedro Alves @ 2020-11-25  1:29 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches

On 11/10/20 9:46 PM, Simon Marchi via Gdb-patches wrote:
> Since we're going to introduce other "displaced step" functions and
> another kind of displaced step closure, make it clear that this is the
> return type of the gdbarch_displaced_step_copy_insn function.
> 
> gdb/ChangeLog:
> 
> 	* infrun.h (get_displaced_step_closure_by_addr): Rename to...
> 	(get_displaced_step_copy_insn_closure_by_addr): ... this.
> 	Update all users.
> 	(displaced_step_closure): Rename to...
> 	(displaced_step_copy_insn_closure): ... this.  Update all users.
> 	(displaced_step_closure_up): Rename to...
> 	(displaced_step_copy_insn_closure_up). ... this.  Update all
> 	users.
> 	(buf_displaced_step_closure): Rename to...
> 	(buf_displaced_step_copy_insn_closure): ... this.  Update all
> 	users.
> 	* infrun.c (get_displaced_step_closure_by_addr): Rename to...
> 	(get_displaced_step_copy_insn_closure_by_addr): ... this.
> 	Update all users.
> 	* aarch64-tdep.c (aarch64_displaced_step_closure): Rename to...
> 	(aarch64_displaced_step_copy_insn_closure): ... this.  Update
> 	all users.
> 	* amd64-tdep.c (amd64_displaced_step_closure): Rename to...
> 	(amd64_displaced_step_copy_insn_closure): ... this.  Update all
> 	users.
> 	* arm-tdep.h (arm_displaced_step_closure): Rename to...
> 	(arm_displaced_step_copy_insn_closure): ... this.  Update all
> 	users.
> 	* i386-tdep.h (i386_displaced_step_closure): Rename to...
> 	(i386_displaced_step_copy_insn_closure): ... this.  Update all
> 	users.
> 	* rs6000-tdep.c (ppc_displaced_step_closure): Rename to...
> 	(ppc_displaced_step_copy_insn_closure): ... this.  Update all
> 	users.
> 	* s390-tdep.c (s390_displaced_step_closure): Rename to...
> 	(s390_displaced_step_copy_insn_closure): ... this.  Update all
> 	users.
> 	* gdbarch.h: Re-generate.
> 	* gdbarch.c: Re-generate.

OK.

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

* Re: [PATCH 05/12] gdb: rename displaced_step_fixup to displaced_step_finish
  2020-11-10 21:46 ` [PATCH 05/12] gdb: rename displaced_step_fixup to displaced_step_finish Simon Marchi
@ 2020-11-25  1:29   ` Pedro Alves
  0 siblings, 0 replies; 44+ messages in thread
From: Pedro Alves @ 2020-11-25  1:29 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches

On 11/10/20 9:46 PM, Simon Marchi via Gdb-patches wrote:
> This is a preparatory patch to reduce a little bit the diff size of the
> main patch later in this series.  It renames the displaced_step_fixup
> function in infrun.c to displaced_step_finish.
> 
> The rationale is to better differentiate the low and high level
> operations.
> 
> We first have the low level operation of writing an instruction to a
> displaced buffer, called "copy_insn".  The mirror low level operation to
> fix up the state after having executed the instruction is "fixup".  The
> high level operation of preparing a thread for a displaced step (which
> includes doing the "copy_insn" and some more bookkeeping) is called
> "prepare" (as in displaced_step_prepare).  The mirror high level
> operation to cleaning up after a displaced step (which includes doing
> the "fixup" and some more bookkeeping) is currently also called "fixup"
> (as in displaced_step_fixup), just like the low level operation.
> 
> I think that choosing a different name for the low and high level
> cleanup operation makes it clearer, hence "finish".
> 
> gdb/ChangeLog:
> 
> 	* infrun.c (displaced_step_fixup): Rename to...
> 	(displaced_step_finish): ... this, update all callers.

OK.

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

* Re: [PATCH 06/12] gdb: introduce status enum for displaced step prepare/finish
  2020-11-10 21:46 ` [PATCH 06/12] gdb: introduce status enum for displaced step prepare/finish Simon Marchi
  2020-11-11 23:36   ` Andrew Burgess
@ 2020-11-25  1:30   ` Pedro Alves
  2020-11-25 13:20     ` Simon Marchi
  1 sibling, 1 reply; 44+ messages in thread
From: Pedro Alves @ 2020-11-25  1:30 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches

On 11/10/20 9:46 PM, Simon Marchi via Gdb-patches wrote:

> --- /dev/null
> +++ b/gdb/displaced-stepping.h
> @@ -0,0 +1,46 @@
> +/* Displaced stepping related things.
> +
> +   Copyright (C) 2020 Free Software Foundation, Inc.
> +
> +   This file is part of GDB.
> +
> +   This program is free software; you can redistribute it and/or modify
> +   it under the terms of the GNU General Public License as published by
> +   the Free Software Foundation; either version 3 of the License, or
> +   (at your option) any later version.
> +
> +   This program is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +   GNU General Public License for more details.
> +
> +   You should have received a copy of the GNU General Public License
> +   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
> +
> +#ifndef DISPLACED_STEPPING_H
> +#define DISPLACED_STEPPING_H
> +
> +enum displaced_step_prepare_status
> +{
> +  /* A displaced stepping buffer was successfully allocated and prepared.  */
> +  DISPLACED_STEP_PREPARE_STATUS_OK,
> +
> +  /* Something bad happened.  */
> +  DISPLACED_STEP_PREPARE_STATUS_ERROR,
> +
> +  /* Not enough resources are available at this time, try again later.  */
> +  DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE,
> +};
> +
> +enum displaced_step_finish_status
> +{
> +  /* Either the instruction was stepped and fixed up, or the specified thread
> +     wasn't executing a displaced step (in which case there's nothing to
> +     finish). */
> +  DISPLACED_STEP_FINISH_STATUS_OK,
> +
> +  /* The thread was executing a displaced step, but didn't complete it.  */
> +  DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED,

There's an ambiguity here that gave me pause -- the enumeration is
called "not executed", but the comment says "was executing".  I'd suggest:

  /* The thread started a displaced step, but didn't complete it.  */



> -/* If we displaced stepped an instruction successfully, adjust
> -   registers and memory to yield the same effect the instruction would
> -   have had if we had executed it at its original address, and return
> -   1.  If the instruction didn't complete, relocate the PC and return
> -   -1.  If the thread wasn't displaced stepping, return 0.  */
> +/* If we displaced stepped an instruction successfully, adjust registers and
> +   memory to yield the same effect the instruction would have had if we had
> +   executed it at its original address, and return
> +   DISPLACED_STEP_PREPARE_STATUS_OK.  If the instruction didn't complete,
> +   relocate the PC and return DISPLACED_STEP_PREPARE_STATUS_NOT_EXECUTED.

DISPLACED_STEP_PREPARE_... -> DISPLACED_STEP_FINISH_...

> -static int
> +   If the thread wasn't displaced stepping, return
> +   DISPLACED_STEP_PREPARE_STATUS_OK as well..  */

Here too.  Also, spurious double period "..".

> +
> +static displaced_step_finish_status
>  displaced_step_finish (thread_info *event_thread, enum gdb_signal signal)
>  {
>    struct displaced_step_inferior_state *displaced
>      = get_displaced_stepping_state (event_thread->inf);
> -  int ret;
>  
>    /* Was this event for the thread we displaced?  */
>    if (displaced->step_thread != event_thread)
> -    return 0;
> +    return DISPLACED_STEP_FINISH_STATUS_OK;
>  


Otherwise OK.

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

* Re: [PATCH 07/12] gdb: pass inferior to get_linux_inferior_data
  2020-11-10 21:46 ` [PATCH 07/12] gdb: pass inferior to get_linux_inferior_data Simon Marchi
@ 2020-11-25  1:30   ` Pedro Alves
  0 siblings, 0 replies; 44+ messages in thread
From: Pedro Alves @ 2020-11-25  1:30 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches

On 11/10/20 9:46 PM, Simon Marchi via Gdb-patches wrote:
> Pass to get_linux_inferior_data the inferior for which we want to obtain
> the linux-specific data, rather than assuming the current inferior.
> This helps slightly reduce the diff in the upcoming main patch.
> 
> Update the sole caller to pass the current inferior.
> 
> gdb/ChangeLog:
> 
> 	* linux-tdep.c (get_linux_inferior_data): Add inferior
> 	parameter.
> 	(linux_vsyscall_range): Pass current inferior.

OK.

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

* Re: [PATCH 08/12] gdb: move displaced stepping types to displaced-stepping.{h, c}
  2020-11-10 21:46 ` [PATCH 08/12] gdb: move displaced stepping types to displaced-stepping.{h, c} Simon Marchi
@ 2020-11-25  1:30   ` Pedro Alves
  0 siblings, 0 replies; 44+ messages in thread
From: Pedro Alves @ 2020-11-25  1:30 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches

On 11/10/20 9:46 PM, Simon Marchi via Gdb-patches wrote:
> Move displaced-stepping related stuff unchanged to displaced-stepping.h
> and displaced-stepping.c.  This helps make the following patch a bit
> smaller and easier to read.
> 
> gdb/ChangeLog:
> 
> 	* Makefile.in (COMMON_SFILES): Add displaced-stepping.c.
> 	* aarch64-tdep.h: Include displaced-stepping.h.
> 	* displaced-stepping.h (struct displaced_step_copy_insn_closure):
> 	Move here.
> 	(displaced_step_copy_insn_closure_up): Move here.
> 	(struct buf_displaced_step_copy_insn_closure): Move here.
> 	(struct displaced_step_inferior_state): Move here.
> 	(debug_displaced): Move here.
> 	(displaced_debug_printf_1): Move here.
> 	(displaced_debug_printf): Move here.
> 	* displaced-stepping.c: New file.
> 	* gdbarch.sh: Include displaced-stepping.h in gdbarch.h.
> 	* gdbarch.h: Re-generate.
> 	* inferior.h: Include displaced-stepping.h.
> 	* infrun.h (debug_displaced): Move to displaced-stepping.h.
> 	(displaced_debug_printf_1): Likewise.
> 	(displaced_debug_printf): Likewise.
> 	(struct displaced_step_copy_insn_closure): Likewise.
> 	(displaced_step_copy_insn_closure_up): Likewise.
> 	(struct buf_displaced_step_copy_insn_closure): Likewise.
> 	(struct displaced_step_inferior_state): Likewise.
> 	* infrun.c (show_debug_displaced): Move to displaced-stepping.c.
> 	(displaced_debug_printf_1): Likewise.
> 	(displaced_step_copy_insn_closure::~displaced_step_copy_insn_closure):
> 	Likewise.
> 	(_initialize_infrun): Don't register "set/show debug displaced".

OK.

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

* Re: [PATCH 09/12] gdb: move displaced stepping logic to gdbarch, allow starting concurrent displaced steps
  2020-11-10 21:46 ` [PATCH 09/12] gdb: move displaced stepping logic to gdbarch, allow starting concurrent displaced steps Simon Marchi
@ 2020-11-25  1:40   ` Pedro Alves
  2020-11-25 19:29     ` Simon Marchi
  0 siblings, 1 reply; 44+ messages in thread
From: Pedro Alves @ 2020-11-25  1:40 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches

Hi,

So overall this is looking pretty good.  A few comments below.

Please also see the comments in response to patch #12.  This patch
causes a surprising slowdown.


On 11/10/20 9:46 PM, Simon Marchi via Gdb-patches wrote:

> - Ask the gdbarch for the displaced step buffer location
> - Save the existing bytes in the displaced step buffer
> - Ask the gdbarch to copy the instruction into the displaced step buffer
> - Set the pc of the thread to the beginning of the displaced step buffer
> 
> Similarly, the "fixup" phase, executed after the instruction was
> successfully single-stepped, is driven by the infrun code (function
> displaced_step_fixup).  The steps are roughly:

displaced_step_fixup -> displaced_step_finish.  And maybe "finish" phase?

> 
> - Restore the original bytes in the displaced stepping buffer
> - Ask the gdbarch to fixup the instruction result (adjust the target's
>   registers or memory to do as if the instruction had been executed in
>   its original location)
> 
> The displaced_step_inferior_state::step_thread field indicates which
> thread (if any) is currently using the displaced stepping buffer, so it
> is used by displaced_step_prepare_throw to check if the displaced
> stepping buffer is free to use or not.
> 
> This patch defers the whole task of preparing and cleaning up after a
> displaced step to the gdbarch.  Two new main gdbarch methods are added,
> with the following sematics:

"sematics" -> "semantics"

> 
>   - gdbarch_displaced_step_prepare: Prepare for the given thread to
>     execute a displaced step of the instruction located at its current PC.
>     Upon return, everything should be ready for GDB to resume the thread
>     (with either a single step or continue, as indicated by
>     gdbarch_displaced_step_hw_singlestep) to make it displaced step the
>     instruction.
> 
>   - gdbarch_displaced_step_finish: Called when the thread stopped after
>     having started a displaced step.  Verify if the instruction was
>     executed, if so apply any fixup required to compensate for the fact
>     that the instruction was executed at different place than its original

"at a different place"

>     pc.  Release any resources that was allocated for this displaced step.

"that were allocated"

>     Upon return, everything should be ready for GDB to resume the
>     thread in its "normal" code path.
> 
> The displaced_step_prepare_throw function now pretty much just offloads
> to gdbarch_displaced_step_prepare and the displaced_step_finish function
> offloads to gdbarch_displaced_step_finish.
> 
> The gdbarch_displaced_step_location method is now unnecessary, so is
> removed.  Indeed, the core of GDB doesn't know how many displaced step
> buffers there are nor where they are.
> 
> To keep the existing behavior for existing architectures, the logic that
> was previously implemented in infrun.c for preparing and finishing a
> displaced step is moved to displaced-stepping.c, to the
> displaced_step_buffer class.  Architectures are modified to implement
> the new gdbarch methods using this class.  The behavior is not expeicted
> to change.

"expeicted" -> "expected"


> @@ -37,6 +43,188 @@ show_debug_displaced (struct ui_file *file, int from_tty,
>    fprintf_filtered (file, _("Displace stepping debugging is %s.\n"), value);
>  }
>  
> +displaced_step_prepare_status
> +displaced_step_buffer::prepare (thread_info *thread, CORE_ADDR &displaced_pc)
> +{

...

> +
> +  m_original_pc = regcache_read_pc (regcache);
> +  displaced_pc = m_addr;
> +
> +  /* Save the original contents of the displaced stepping buffer.  */
> +  m_saved_copy.resize (len);
> +
> +  int status = target_read_memory (m_addr, m_saved_copy.data (), len);
> +  if (status != 0)
> +    throw_error (MEMORY_ERROR,
> +		 _("Error accessing memory address %s (%s) for "
> +		   "displaced-stepping scratch space."),
> +		 paddress (arch, m_addr), safe_strerror (status));
> +
> +  displaced_debug_printf ("saved %s: %s",
> +			  paddress (arch, m_addr),
> +			  displaced_step_dump_bytes
> +			    (m_saved_copy.data (), len).c_str ());
> +
> +  m_copy_insn_closure = gdbarch_displaced_step_copy_insn (arch,
> +							  m_original_pc,
> +							  m_addr,
> +							  regcache);
> +  if (m_copy_insn_closure == nullptr)
> +    {
> +      /* The architecture doesn't know how or want to displaced step
> +        this instruction or instruction sequence.  Fallback to
> +        stepping over the breakpoint in-line.  */
> +      return DISPLACED_STEP_PREPARE_STATUS_ERROR;
> +    }
> +
> +  try
> +    {
> +      /* Resume execution at the copy.  */
> +      regcache_write_pc (regcache, m_addr);
> +    }
> +  catch (...)

I'd rather we don't use 'catch (...)' throughout gdb.  It swallows
too much.  We should catch gdb_exception_error in most cases or
gdb_exception when we also want to catch Ctrl-C/QUIT.  If something
throws something else, I'd rather not swallow it, since it's most
probably a bug.

I'm a bit confused on error handling here.  Before the patch, we used
displaced_step_reset_cleanup, but didn't swallow the error.  After the patch,
the exception is swallowed and DISPLACED_STEP_PREPARE_STATUS_ERROR is
returned.  Was that on purpose?

> +    {
> +      /* Failed to write the PC.  Release the architecture's displaced
> +         stepping resources and the thread's displaced stepping state.  */
> +      m_copy_insn_closure.reset ();
> +
> +      return DISPLACED_STEP_PREPARE_STATUS_ERROR;
> +    }
> +
> +  /* This marks the buffer as being in use.  */
> +  m_current_thread = thread;
> +
> +  return DISPLACED_STEP_PREPARE_STATUS_OK;
> +}




>  
> @@ -94,37 +99,78 @@ struct displaced_step_inferior_state
>    /* Put this object back in its original state.  */
>    void reset ()
>    {
> -    failed_before = 0;
> -    step_thread = nullptr;
> -    step_gdbarch = nullptr;
> -    step_closure.reset ();
> -    step_original = 0;
> -    step_copy = 0;
> -    step_saved_copy.clear ();
> +    failed_before = false;
>    }
>  
>    /* True if preparing a displaced step ever failed.  If so, we won't
>       try displaced stepping for this inferior again.  */
> -  int failed_before;
> +  bool failed_before;
> +};
> +
> +/* Per-thread displaced stepping state.  */
> +
> +struct displaced_step_thread_state
> +{
> +  /* Return true if this thread is currently executing a displaced step.  */
> +  bool in_progress () const
> +  {
> +    return m_original_gdbarch != nullptr;
> +  }
> +
> +  /* Return the gdbarch of the thread prior to the step.  */
> +  gdbarch *get_original_gdbarch () const
> +  {
> +    return m_original_gdbarch;
> +  }
> +
> +  /* Mark this thread as currently executing a displaced step.
> +
> +     ORIGINAL_GDBARCH is the current gdbarch of the thread (before the step
> +     is executed).  */
> +  void set (gdbarch *original_gdbarch)
> +  {
> +    m_original_gdbarch = original_gdbarch;
> +  }
> +
> +  /* Mark this thread as no longer executing a displaced step.  */
> +  void reset ()
> +  {
> +    m_original_gdbarch = nullptr;
> +  }
> +
> +private:
> +  gdbarch *m_original_gdbarch = nullptr;
> +};
> +
> +/* Manage access to a single displaced stepping buffer.  */
> +
> +struct displaced_step_buffer
> +{
> +  displaced_step_buffer (CORE_ADDR buffer_addr)

explicit.

> +    : m_addr (buffer_addr)
> +  {}
> +
> +  displaced_step_prepare_status prepare (thread_info *thread,
> +					 CORE_ADDR &displaced_pc);
> +
> +  displaced_step_finish_status finish (gdbarch *arch, thread_info *thread,
> +				       gdb_signal sig);
> +
> +  const displaced_step_copy_insn_closure *
> +    copy_insn_closure_by_addr (CORE_ADDR addr);
>  
> -  /* If this is not nullptr, this is the thread carrying out a
> -     displaced single-step in process PID.  This thread's state will
> -     require fixing up once it has completed its step.  */
> -  thread_info *step_thread;
> +  void restore_in_ptid (ptid_t ptid);
>  
> -  /* The architecture the thread had when we stepped it.  */
> -  gdbarch *step_gdbarch;
> +private:
>  
> -  /* The closure provided gdbarch_displaced_step_copy_insn, to be used
> -     for post-step cleanup.  */
> -  displaced_step_copy_insn_closure_up step_closure;
> +  CORE_ADDR m_original_pc = 0;
> +  const CORE_ADDR m_addr;
>  
> -  /* The address of the original instruction, and the copy we
> -     made.  */
> -  CORE_ADDR step_original, step_copy;
> +  /* If set, the thread currently using the buffer.  */
> +  thread_info *m_current_thread = nullptr;
>  
> -  /* Saved contents of copy area.  */
> -  gdb::byte_vector step_saved_copy;
> +  gdb::byte_vector m_saved_copy;
> +  displaced_step_copy_insn_closure_up m_copy_insn_closure;

Missing comments?

>  };
>  
>  #endif /* DISPLACED_STEPPING_H */

> @@ -1554,9 +1549,9 @@ show_can_use_displaced_stepping (struct ui_file *file, int from_tty,
>  static bool
>  gdbarch_supports_displaced_stepping (gdbarch *arch)
>  {
> -  /* Only check for the presence of step_copy_insn.  Other required methods
> -     are checked by the gdbarch validation.  */
> -  return gdbarch_displaced_step_copy_insn_p (arch);
> +  /* Only check for the presence of `prepare`.  `finish` is required by the
> +     gdbarch verification to be provided if `prepare` is provided.  */

This reads a little funny to me.  I'd suggest:

  /* Only check for the presence of `prepare`.  The gdbarch verification ensures
     that if `prepare` is provided, so is `finish`.  */

> +  return gdbarch_displaced_step_prepare_p (arch);
>  }
>  
>  /* Return non-zero if displaced stepping can/should be used to step

> @@ -1669,96 +1662,52 @@ displaced_step_prepare_throw (thread_info *tp)
>       jump/branch).  */
>    tp->control.may_range_step = 0;
>  
> -  /* We have to displaced step one thread at a time, as we only have
> -     access to a single scratch space per inferior.  */
> -
> -  displaced_step_inferior_state *displaced
> -    = get_displaced_stepping_state (tp->inf);
> -
> -  if (displaced->step_thread != nullptr)
> -    {
> -      /* Already waiting for a displaced step to finish.  Defer this
> -	 request and place in queue.  */
> -
> -      displaced_debug_printf ("deferring step of %s",
> -			      target_pid_to_str (tp->ptid).c_str ());
> -
> -      global_thread_step_over_chain_enqueue (tp);
> -      return DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE;
> -    }
> -  else
> -    displaced_debug_printf ("stepping %s now",
> -			    target_pid_to_str (tp->ptid).c_str ());
> -
> -  displaced_step_reset (displaced);
> +  /* We are about to start a displaced step for this thread.  If one is already
> +     in progress, something's wrong..  */

Double period.

> +  gdb_assert (!disp_step_thread_state->in_progress ());
>  
>    scoped_restore_current_thread restore_thread;
>  
>    switch_to_thread (tp);
>  
> -  original = regcache_read_pc (regcache);
> +  CORE_ADDR original_pc = regcache_read_pc (regcache);
> +  CORE_ADDR displaced_pc;
>  
> -  copy = gdbarch_displaced_step_location (gdbarch);
> -  len = gdbarch_max_insn_length (gdbarch);
> +  displaced_step_prepare_status status =
> +    gdbarch_displaced_step_prepare (gdbarch, tp, displaced_pc);

" = " on the next line.

>  
> -  if (breakpoint_in_range_p (aspace, copy, len))
> +  if (status == DISPLACED_STEP_PREPARE_STATUS_ERROR)


> @@ -1934,14 +1831,22 @@ static step_over_what thread_still_needs_step_over (struct thread_info *tp);
>  static bool
>  start_step_over (void)
>  {
> -  struct thread_info *tp, *next;
> +  thread_info *next;
> +  bool started = false;
>  
>    /* Don't start a new step-over if we already have an in-line
>       step-over operation ongoing.  */
>    if (step_over_info_valid_p ())
> -    return false;
> +    return started;

I'd move the "bool started = false;" below this if line and continue
writing explicit "return false" here, as it's less indirection.

> +
> +  /* Steal the global thread step over chain.  */

It would be good expand the command explaining _why_ steal.

> +  thread_info *threads_to_step = global_thread_step_over_chain_head;
> +  global_thread_step_over_chain_head = NULL;
>  
> -  for (tp = global_thread_step_over_chain_head; tp != NULL; tp = next)
> +  infrun_debug_printf ("stealing global queue of threads to step, length = %d",
> +		       thread_step_over_chain_length (threads_to_step));
> +
> +  for (thread_info *tp = threads_to_step; tp != NULL; tp = next)
>      {
>        struct execution_control_state ecss;
>        struct execution_control_state *ecs = &ecss;


> @@ -2005,13 +1902,27 @@ start_step_over (void)
>        if (!ecs->wait_some_more)
>  	error (_("Command aborted."));
>  
> -      gdb_assert (tp->resumed);
> +      /* If the thread's step over could not be initiated, it was re-added
> +	 to the global step over chain.  */
> +      if (tp->resumed)
> +	{
> +	  infrun_debug_printf ("[%s] was resumed.",
> +			       target_pid_to_str (tp->ptid).c_str ());
> +	  gdb_assert (!thread_is_in_step_over_chain (tp));
> +	}
> +      else
> +	{
> +	  infrun_debug_printf ("[%s] was NOT resumed.",
> +			       target_pid_to_str (tp->ptid).c_str ());
> +	  gdb_assert (thread_is_in_step_over_chain (tp));
> +	}
>  
>        /* If we started a new in-line step-over, we're done.  */
>        if (step_over_info_valid_p ())
>  	{
>  	  gdb_assert (tp->control.trap_expected);
> -	  return true;
> +	  started = true;
> +	  break;
>  	}
>  
>        if (!target_is_non_stop_p ())
> @@ -2024,7 +1935,8 @@ start_step_over (void)
>  	  /* With remote targets (at least), in all-stop, we can't
>  	     issue any further remote commands until the program stops
>  	     again.  */
> -	  return true;
> +	  started = true;
> +	  break;
>  	}
>  
>        /* Either the thread no longer needed a step-over, or a new
> @@ -2033,7 +1945,30 @@ start_step_over (void)
>  	 displaced step on a thread of other process. */
>      }
>  
> -  return false;
> +    /* If there are threads left in the THREADS_TO_STEP list, but we have
> +       detected that we can't start anything more, put back these threads
> +       in the global list.  */

Do we also need to do this if an exception happens to escape the function?
We might end up pretty bonkers anyhow if that happens, though...

> +    if (threads_to_step == NULL)
> +      infrun_debug_printf ("step-over queue now empty");
> +    else
> +      {
> +	infrun_debug_printf ("putting back %d threads to step in global queue",
> +			     thread_step_over_chain_length (threads_to_step));
> +
> +	while (threads_to_step != nullptr)
> +	  {
> +	    thread_info *thread = threads_to_step;
> +
> +	    /* Remove from that list.  */
> +	    thread_step_over_chain_remove (&threads_to_step, thread);
> +
> +	    /* Add to global list.  */
> +	    global_thread_step_over_chain_enqueue (thread);
> +

Spurious empty line.  But, did you look into splicing the whole threads_to_step
chain into the global chain in O(1), with just some prev/next pointer
adjustments?

> +	  }
> +      }
> +
> +  return started;
>  }
>  
>  /* Update global variables holding ptids to hold NEW_PTID if they were
> @@ -3618,18 +3553,16 @@ prepare_for_detach (void)
>    struct inferior *inf = current_inferior ();
>    ptid_t pid_ptid = ptid_t (inf->pid);
>  
> -  displaced_step_inferior_state *displaced = get_displaced_stepping_state (inf);
> -
>    /* Is any thread of this process displaced stepping?  If not,
>       there's nothing else to do.  */
> -  if (displaced->step_thread == nullptr)
> +  if (displaced_step_in_progress (inf))
>      return;
>  
>    infrun_debug_printf ("displaced-stepping in-process while detaching");
>  
>    scoped_restore restore_detaching = make_scoped_restore (&inf->detaching, true);
>  
> -  while (displaced->step_thread != nullptr)
> +  while (displaced_step_in_progress (inf))
>      {
>        struct execution_control_state ecss;
>        struct execution_control_state *ecs;
> @@ -5293,25 +5226,23 @@ handle_inferior_event (struct execution_control_state *ecs)
>        {
>  	struct regcache *regcache = get_thread_regcache (ecs->event_thread);
>  	struct gdbarch *gdbarch = regcache->arch ();
> +	inferior *parent_inf = find_inferior_ptid (ecs->target, ecs->ptid);
> +
> +	if (ecs->ws.kind == TARGET_WAITKIND_FORKED)
> +	  {
> +	    /* Restore in the child process any displaced stepping buffers that
> +	       were in use at the time of the fork.  */
> +	    gdbarch_displaced_step_restore_all_in_ptid
> +	      (gdbarch, parent_inf, ecs->ws.value.related_pid);
> +	  }

Why was this moved out of the displaced_step_in_progress_thread check
below?

>  
>  	/* If checking displaced stepping is supported, and thread
>  	   ecs->ptid is displaced stepping.  */
>  	if (displaced_step_in_progress_thread (ecs->event_thread))
>  	  {
> -	    struct inferior *parent_inf
> -	      = find_inferior_ptid (ecs->target, ecs->ptid);
>  	    struct regcache *child_regcache;
>  	    CORE_ADDR parent_pc;
>  
> -	    if (ecs->ws.kind == TARGET_WAITKIND_FORKED)
> -	      {
> -		struct displaced_step_inferior_state *displaced
> -		  = get_displaced_stepping_state (parent_inf);
> -
> -		/* Restore scratch pad for child process.  */
> -		displaced_step_restore (displaced, ecs->ws.value.related_pid);
> -	      }
> -
>  	    /* GDB has got TARGET_WAITKIND_FORKED or TARGET_WAITKIND_VFORKED,
>  	       indicating that the displaced stepping of syscall instruction
>  	       has been done.  Perform cleanup for parent process here.  Note
> diff --git a/gdb/infrun.h b/gdb/infrun.h
> index c83cb333083..d5e6d279f1a 100644
> --- a/gdb/infrun.h
> +++ b/gdb/infrun.h
> @@ -226,9 +226,6 @@ extern void clear_exit_convenience_vars (void);
>  /* Dump LEN bytes at BUF in hex to a string and return it.  */
>  extern std::string displaced_step_dump_bytes (const gdb_byte *buf, size_t len);
>  
> -extern struct displaced_step_copy_insn_closure *
> -  get_displaced_step_copy_insn_closure_by_addr (CORE_ADDR addr);
> -
>  extern void update_observer_mode (void);
>  
>  extern void signal_catch_update (const unsigned int *);
> diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c
> index c57181765e0..f3d1ccb4d1c 100644
> --- a/gdb/linux-tdep.c
> +++ b/gdb/linux-tdep.c
> @@ -199,6 +199,9 @@ struct linux_info
>       yet.  Positive if we tried looking it up, and found it.  Negative
>       if we tried looking it up but failed.  */
>    int vsyscall_range_p = 0;
> +
> +  /* Inferior's displaced step buffer.  */
> +  gdb::optional<displaced_step_buffer> disp_step_buf;
>  };
>  
>  /* Per-inferior data key.  */
> @@ -2530,6 +2533,61 @@ linux_displaced_step_location (struct gdbarch *gdbarch)
>    return addr;
>  }
>  
> +/* Implementation gdbarch_displaced_step_prepare.  */

"Implementation of" ?

> +
> +displaced_step_prepare_status
> +linux_displaced_step_prepare (gdbarch *arch, thread_info *thread,
> +			      CORE_ADDR &displaced_pc)
> +{
> +  linux_info *per_inferior = get_linux_inferior_data (thread->inf);
> +
> +  if (!per_inferior->disp_step_buf.has_value ())
> +    {
> +      CORE_ADDR disp_step_buf_addr
> +	= linux_displaced_step_location (thread->inf->gdbarch);
> +
> +      per_inferior->disp_step_buf.emplace (disp_step_buf_addr);
> +    }
> +
> +  return per_inferior->disp_step_buf->prepare (thread, displaced_pc);
> +}
> +
> +/* Implementation gdbarch_displaced_step_finish.  */

Ditto.

> +
> +displaced_step_finish_status
> +linux_displaced_step_finish (gdbarch *arch, thread_info *thread, gdb_signal sig)
> +{
> +  linux_info *per_inferior = get_linux_inferior_data (thread->inf);
> +
> +  gdb_assert (per_inferior->disp_step_buf.has_value ());
> +
> +  return per_inferior->disp_step_buf->finish (arch, thread, sig);
> +}
> +
> +const displaced_step_copy_insn_closure *
> +linux_displaced_step_copy_insn_closure_by_addr (inferior *inf, CORE_ADDR addr)

Ditto on the immaginary comment.  :-)

> +{
> +  linux_info *per_inferior = linux_inferior_data.get (inf);
> +
> +  if (per_inferior == nullptr
> +      || !per_inferior->disp_step_buf.has_value ())
> +    return nullptr;
> +
> +  return per_inferior->disp_step_buf->copy_insn_closure_by_addr (addr);
> +}
> +
> +void
> +linux_displaced_step_restore_all_in_ptid (inferior *parent_inf, ptid_t ptid)
> +{
> +  linux_info *per_inferior = linux_inferior_data.get (parent_inf);
> +

...

>  /* See gdbthread.h.  */
> @@ -403,9 +411,32 @@ thread_is_in_step_over_chain (struct thread_info *tp)
>  
>  /* See gdbthread.h.  */
>  
> +int
> +thread_step_over_chain_length (thread_info *tp)
> +{
> +  if (tp == nullptr)
> +    return 0;
> +
> +  int num = 1;

Should we add:

  gdb_assert (thread_is_in_step_over_chain (tp));

?

But then again, it's a bit odd to allow tp == nullptr,
but not allow tp->step_over_next == nullptr?

> +  thread_info *iter = tp->step_over_next;
> +
> +  while (iter != tp)
> +    {
> +      num++;
> +      iter = iter->step_over_next;
> +    }

This seems to be begging for a "for":

  int num = 1;
  for (thread_info *iter = tp->step_over_next;
       iter != tp;
       iter = iter->step_over_next)
    num++;

> +
> +  return num;
> +}


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

* Re: [PATCH 10/12] gdb: change linux gdbarch data from post to pre-init
  2020-11-10 21:46 ` [PATCH 10/12] gdb: change linux gdbarch data from post to pre-init Simon Marchi
@ 2020-11-25  1:41   ` Pedro Alves
  0 siblings, 0 replies; 44+ messages in thread
From: Pedro Alves @ 2020-11-25  1:41 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches

On 11/10/20 9:46 PM, Simon Marchi via Gdb-patches wrote:
> The following patch will need to fill a field in linux_gdbarch_data
> while the gdbarch is being built.  linux_gdbarch_data is currently
> allocated as a post-init gdbarch data, meaning it's not possible to fill
> it before the gdbarch is completely initialized.  Change it to a
> pre-init gdbarch data to allow this.
> 
> The init_linux_gdbarch_data function doesn't use the created gdbarch,
> it only allocates the linux_gdbarch_data structure on the gdbarch's
> obstack, so the change is trivial.
> 
> gdb/ChangeLog:
> 
> 	* linux-tdep.c (init_linux_gdbarch_data): Change parameter to
> 	obkstack.

"obkstack" -> "obstack".

> 	(_initialize_linux_tdep): Register pre-init gdb data instead of
> 	post-init.

OK.

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

* Re: [PATCH 11/12] gdb: make displaced stepping implementation capable of managing multiple buffers
  2020-11-10 21:46 ` [PATCH 11/12] gdb: make displaced stepping implementation capable of managing multiple buffers Simon Marchi
@ 2020-11-25  1:41   ` Pedro Alves
  2020-11-30 18:58     ` Simon Marchi
  2020-11-30 19:01     ` Simon Marchi
  0 siblings, 2 replies; 44+ messages in thread
From: Pedro Alves @ 2020-11-25  1:41 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches

A few minor comments below.  Otherwise LGTM.

On 11/10/20 9:46 PM, Simon Marchi via Gdb-patches wrote:

> @@ -44,86 +44,114 @@ show_debug_displaced (struct ui_file *file, int from_tty,
>  }
>  
>  displaced_step_prepare_status
> -displaced_step_buffer::prepare (thread_info *thread, CORE_ADDR &displaced_pc)
> +displaced_step_buffers::prepare (thread_info *thread, CORE_ADDR &displaced_pc)
>  {
>    gdb_assert (!thread->displaced_step_state.in_progress ());
>  
> -  /* Is a thread currently using the buffer?  */
> -  if (m_current_thread != nullptr)
> -    {
> -      /* If so, it better not be this thread.  */
> -      gdb_assert (thread != m_current_thread);
> -      return DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE;
> -    }
> +  /* Sanity check: the thread should not be using a buffer at this point.  */
> +  for (displaced_step_buffer &buf : m_buffers)
> +    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_max_insn_length (arch);
>  
> -  if (breakpoint_in_range_p (aspace, m_addr, len))
> -    {
> -      /* There's a breakpoint set in the scratch pad location range
> -	 (which is usually around the entry point).  We'd either
> -	 install it before resuming, which would overwrite/corrupt the
> -	 scratch pad, or if it was already inserted, this displaced
> -	 step would overwrite it.  The latter is OK in the sense that
> -	 we already assume that no thread is going to execute the code
> -	 in the scratch pad range (after initial startup) anyway, but
> -	 the former is unacceptable.  Simply punt and fallback to
> -	 stepping over this breakpoint in-line.  */
> -      displaced_debug_printf ("breakpoint set in scratch pad.  "
> -			      "Stepping over breakpoint in-line instead.");
> +  /* Search for an unused buffer.  */
> +  displaced_step_buffer *buffer = nullptr;
> +  displaced_step_prepare_status fail_status
> +    = DISPLACED_STEP_PREPARE_STATUS_ERROR;
>  
> -      return DISPLACED_STEP_PREPARE_STATUS_ERROR;
> +  for (displaced_step_buffer &candidate : m_buffers)
> +    {
> +      bool bp_in_range = breakpoint_in_range_p (aspace, candidate.addr, len);
> +      bool is_free = candidate.current_thread == nullptr;
> +
> +      if (!bp_in_range)
> +        {
> +          if (is_free)

tabs vs spaces.

> +	    {
> +	      buffer = &candidate;
> +	      break;
> +	    }
> +	  else
> +	    {
> +	      /* This buffer would be suitable, but it's used right now.  */
> +	      fail_status = DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE;
> +	    }
> +        }

tabs vs spaces.

> +      else
> +	{
> +	  /* There's a breakpoint set in the scratch pad location range
> +	     (which is usually around the entry point).  We'd either
> +	     install it before resuming, which would overwrite/corrupt the
> +	     scratch pad, or if it was already inserted, this displaced
> +	     step would overwrite it.  The latter is OK in the sense that
> +	     we already assume that no thread is going to execute the code
> +	     in the scratch pad range (after initial startup) anyway, but
> +	     the former is unacceptable.  Simply punt and fallback to
> +	     stepping over this breakpoint in-line.  */
> +	  displaced_debug_printf ("breakpoint set in displaced stepping "
> +				  "buffer at %s, can't use.",
> +				  paddress (arch, candidate.addr));
> +	}




> -/* Manage access to a single displaced stepping buffer.  */
> +/* Control access to multiple displaced stepping buffers at fixed addresses.  */
>  
> -struct displaced_step_buffer
> +struct displaced_step_buffers
>  {
> -  displaced_step_buffer (CORE_ADDR buffer_addr)
> -    : m_addr (buffer_addr)
> -  {}
> +  displaced_step_buffers (gdb::array_view<CORE_ADDR> buffer_addrs)
> +  {
> +    gdb_assert (buffer_addrs.size () > 0);
> +
> +    for (CORE_ADDR buffer_addr : buffer_addrs)
> +      m_buffers.emplace_back (buffer_addr);

Call:

      m_buffers.reserve (buffer_addrs.size ());

before.


But even better, the caller is allocating a temporary std::vector
and passing that:

 +      std::vector<CORE_ADDR> buffers;
 +      for (int i = 0; i < gdbarch_data->num_disp_step_buffers; i++)
 +       buffers.push_back (disp_step_buf_addr + i * buf_len);

So how about making the ctor above be instead:

  displaced_step_buffers (std::vector<CORE_ADDR> &&buffer_addrs);

and make the caller move the vector into displaced_step_buffers?

 +      per_inferior->disp_step_bufs.emplace (std::move (buffers));



> +  }
>  
>    displaced_step_prepare_status prepare (thread_info *thread,
>  					 CORE_ADDR &displaced_pc);
> @@ -163,14 +168,33 @@ struct displaced_step_buffer
>  
>  private:
>  
> -  CORE_ADDR m_original_pc = 0;
> -  const CORE_ADDR m_addr;
> +  /* State of a single buffer.  */
> +
> +  struct displaced_step_buffer
> +  {
> +    displaced_step_buffer (CORE_ADDR addr)

"explicit".

> +      : addr (addr)
> +    {}
> +
> +    const CORE_ADDR addr;
> +
> +    /* The original PC of the instruction currently begin stepped.  */

"begin" -> "being" ?

> +    CORE_ADDR original_pc = 0;
> +
> +    /* If set, the thread currently using the buffer.  If unset, the buffer is not
> +       used.  */
> +    thread_info *current_thread = nullptr;
> +
> +    /* Saved copy of the bytes in the displaced buffer, to be restored once the
> +       buffer is no longer used.  */
> +    gdb::byte_vector saved_copy;
>  

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

* Re: [PATCH 12/12] gdb: use two displaced step buffers on amd64/Linux
  2020-11-10 21:46 ` [PATCH 12/12] gdb: use two displaced step buffers on amd64/Linux Simon Marchi
@ 2020-11-25  1:42   ` Pedro Alves
  2020-11-25  6:26     ` Simon Marchi
  0 siblings, 1 reply; 44+ messages in thread
From: Pedro Alves @ 2020-11-25  1:42 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches

On 11/10/20 9:46 PM, Simon Marchi via Gdb-patches wrote:
> As observed on a binary compiled on AMD64 Ubuntu 20.04, against glibc
> 2.31 (I think it's the libc that provides this startup code, right?),

Right.  This is crt0.o.  It's sysdeps/x86_64/start.S in this case:

  https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/x86_64/start.S;h=7477b632f7ad6ffe6cd1975cdc4ec2317f11a6c3;hb=HEAD

> there are enough bytes at the executable's entry point to hold more than
> one displaced step buffer.  gdbarch_max_insn_length is 16, and the
> code at _start looks like:
> 
> 0000000000001040 <_start>:
>     1040:       f3 0f 1e fa             endbr64
>     1044:       31 ed                   xor    %ebp,%ebp
>     1046:       49 89 d1                mov    %rdx,%r9
>     1049:       5e                      pop    %rsi
>     104a:       48 89 e2                mov    %rsp,%rdx
>     104d:       48 83 e4 f0             and    $0xfffffffffffffff0,%rsp
>     1051:       50                      push   %rax
>     1052:       54                      push   %rsp
>     1053:       4c 8d 05 56 01 00 00    lea    0x156(%rip),%r8        # 11b0 <__libc_csu_fini>
>     105a:       48 8d 0d df 00 00 00    lea    0xdf(%rip),%rcx        # 1140 <__libc_csu_init>
>     1061:       48 8d 3d c1 00 00 00    lea    0xc1(%rip),%rdi        # 1129 <main>
>     1068:       ff 15 72 2f 00 00       callq  *0x2f72(%rip)        # 3fe0 <__libc_start_main@GLIBC_2.2.5>
>     106e:       f4                      hlt
>     106f:       90                      nop
> 
> The two buffers would occupy [0x1040, 0x1060).
> 
> I checked on Alpine, which uses the musl C library, the startup code
> looks like:
> 
> 0000000000001048 <_start>:
>     1048:       48 31 ed                xor    %rbp,%rbp
>     104b:       48 89 e7                mov    %rsp,%rdi
>     104e:       48 8d 35 e3 2d 00 00    lea    0x2de3(%rip),%rsi        # 3e38 <_DYNAMIC>
>     1055:       48 83 e4 f0             and    $0xfffffffffffffff0,%rsp
>     1059:       e8 00 00 00 00          callq  105e <_start_c>
> 
> 000000000000105e <_start_c>:
>     105e:       48 8b 37                mov    (%rdi),%rsi
>     1061:       48 8d 57 08             lea    0x8(%rdi),%rdx
>     1065:       45 31 c9                xor    %r9d,%r9d
>     1068:       4c 8d 05 47 01 00 00    lea    0x147(%rip),%r8        # 11b6 <_fini>
>     106f:       48 8d 0d 8a ff ff ff    lea    -0x76(%rip),%rcx        # 1000 <_init>
>     1076:       48 8d 3d 0c 01 00 00    lea    0x10c(%rip),%rdi        # 1189 <main>
>     107d:       e9 9e ff ff ff          jmpq   1020 <__libc_start_main@plt>
> 
> Even though there's a _start_c symbol, it all appears to be code that
> runs once at the very beginning of the program, so it looks fine if the
> two buffers occupy [0x1048, 0x1068).
> 
> One important thing I discovered while doing this is that when debugging
> a dynamically-linked executable, breakpoints in the shared library
> loader are hit before executing the _start code, and these breakpoints
> may be displaced-stepped.  So it's very important that the buffer bytes
> are restored properly after doing the displaced steps, otherwise the
> _start code will be corrupted once we try to execute it.
> 
> Another thing that made me think about is that library constructors (as
> in `__attribute__((constructor))`) run before _start.  And they are free
> to spawn threads.  What if one of these threads executes a displaced
> step, therefore changing the bytes at _start, while the main thread
> executes _start?  That doesn't sound good and I don't know how we could
> prevent it.  But this is a problem that predates the current patch.

Maybe we should put the buffer/buffers at the beginning of the
dynamic loader instead.

> 
> Even when stress-testing the implementation, by making many threads do
> displaced steps over and over, I didn't see a significant performance (I
> confirmed that the two buffers were used by checking the "set debug
> displaced" logs though).  However, this patch mostly helps make the
> feature testable by anybody with an AMD64/Linux machine, so I think it's
> useful.

It should speed up the use case of multiple threads hitting a conditional
breakpoint that evals false at the same time.  The more buffers, the less
time threads need to spend waiting for their turn in the displaced stepping
queue.  If that doesn't speed things up, then probably something isn't quite
right.

/me gives it a try.

Here's a testcase that can be used to observe speed and fairness.
It has 10 threads running a single tight loop for 3 seconds.  After
the 3 seconds, the threads exit and the number of iterations each
thread managed to do is printed.  The idea is to set a breakpoint
with a condition that evals false on the loop.  The higher the
number, the better.  

I compiled it with:

 $ gcc disp-step-buffers-test.c -o disp-step-buffers-test -g3 -O2 -pthread

Here's a run outside gdb:

 $ /tmp/disp-step-buffers-test
 thread 0, count 12785417966
 thread 1, count 12784090476
 thread 2, count 12773373753
 thread 3, count 12782542707
 thread 4, count 12767835404
 thread 5, count 12754382637
 thread 6, count 12783454775
 thread 7, count 12605966064
 thread 8, count 12635255271
 thread 9, count 12783436261

Here's the test:

$ cat /tmp/disp-step-buffers-test.c 
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

#define NUM_THREADS 10

static pthread_t child_thread[NUM_THREADS];
static unsigned long long counters[NUM_THREADS];
static volatile int done;

static void *
child_function (void *arg)
{
  while (!done)
    counters[(long) arg]++;   // set breakpoint here
  return NULL;
}

int
main (void)
{
  long i;

  for (i = 0; i < NUM_THREADS; i++)
    pthread_create (&child_thread[i], NULL, child_function, (void *) i);

  sleep (3);

  done = 1;

  for (i = 0; i < NUM_THREADS; i++)
    pthread_join (child_thread[i], NULL);

  for (i = 0; i < NUM_THREADS; i++)
    printf ("thread %ld, count %llu\n", i, counters[i]);
  return 0;
}

And here are the results under gdb.

Test #1, pristine master (119e99bb7f50):

 (gdb) b 16 if 0
 Breakpoint 1 at 0x1290: file disp-step-buffers-test.c, line 16.
 (gdb) r
 Starting program: /tmp/disp-step-buffers-test 
 thread 0, count 1661
 thread 1, count 1663
 thread 2, count 1646
 thread 3, count 1663
 thread 4, count 1622
 thread 5, count 1661
 thread 6, count 1659
 thread 7, count 1662
 thread 8, count 1660
 thread 9, count 1660
 [Inferior 1 (process 18852) exited normally]
 (gdb) 

Test #2, patches 1 through 11 applied, i.e., 1 buffer:

 (gdb) b 16 if 0
 Breakpoint 1 at 0x1290: file disp-step-buffers-test.c, line 16.
 (gdb) r
 Starting program: /tmp/disp-step-buffers-test 
 ...
 thread 0, count 966
 thread 1, count 950
 thread 2, count 951
 thread 3, count 950
 thread 4, count 946
 thread 5, count 948
 thread 6, count 946
 thread 7, count 979
 thread 8, count 951
 thread 9, count 966
 [Inferior 1 (process 16099) exited normally]
 (gdb)

Test #3, patches 1 through 12 applied, i.e., 2 buffers:

 (gdb) r
 Starting program: /tmp/disp-step-buffers-test 
 ...
 thread 0, count 1124
 thread 1, count 1128
 thread 2, count 1127
 thread 3, count 1123
 thread 4, count 1121
 thread 5, count 1125
 thread 6, count 1126
 thread 7, count 1126
 thread 8, count 1122
 thread 9, count 1122
 [Inferior 1 (process 14983) exited normally]

Wow, test #2 was surprising, it's twice as slow as current
master.  That was unexpected.

Test #3 with the two buffers improves the speed a bit, but still
quite behind current master.  I think this should be sorted out,
at least understood.

I checked, and it's patch #9 the one that introduces the slowdown:

 gdb: move displaced stepping logic to gdbarch, allow starting concurrent displaced steps

so, I'd suspect some new linear thread walks are causing this?
E.g., displaced_step_in_progress.  Maybe we need to keep counters
of number of threads displaced stepping instead of walking all threads,
for example.  I didn't expect this to be observable with only 10 threads.
I'd expect syscall time / ptrace, etc. to dominate.  Maybe it's
something else?

The above numbers were with an -O0 gdb.  I tried again against a gdb built
at -O2, and the absolute numbers were of course better, but the
slowdown is still observed.  I also tried with different numbers
of threads.  Here's what I see:

|threads | master | patch #11 | patch #12|
|----------------------------------------|
|     10 | 3959   |      2847 |      3055|
|     20 | 1806   |      1055 |      1134|
|     50 |  690   |       258 |       258|

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

* Re: [PATCH 12/12] gdb: use two displaced step buffers on amd64/Linux
  2020-11-25  1:42   ` Pedro Alves
@ 2020-11-25  6:26     ` Simon Marchi
  2020-11-25 20:07       ` Simon Marchi
  0 siblings, 1 reply; 44+ messages in thread
From: Simon Marchi @ 2020-11-25  6:26 UTC (permalink / raw)
  To: Pedro Alves, Simon Marchi, gdb-patches

On 2020-11-24 8:42 p.m., Pedro Alves wrote:
>> Even when stress-testing the implementation, by making many threads do
>> displaced steps over and over, I didn't see a significant performance (I
>> confirmed that the two buffers were used by checking the "set debug
>> displaced" logs though).  However, this patch mostly helps make the
>> feature testable by anybody with an AMD64/Linux machine, so I think it's
>> useful.
>
> It should speed up the use case of multiple threads hitting a conditional
> breakpoint that evals false at the same time.  The more buffers, the less
> time threads need to spend waiting for their turn in the displaced stepping
> queue.  If that doesn't speed things up, then probably something isn't quite
> right.
>
> /me gives it a try.
>
> Here's a testcase that can be used to observe speed and fairness.
> It has 10 threads running a single tight loop for 3 seconds.  After
> the 3 seconds, the threads exit and the number of iterations each
> thread managed to do is printed.  The idea is to set a breakpoint
> with a condition that evals false on the loop.  The higher the
> number, the better.
>
> I compiled it with:
>
>  $ gcc disp-step-buffers-test.c -o disp-step-buffers-test -g3 -O2 -pthread
>
> Here's a run outside gdb:
>
>  $ /tmp/disp-step-buffers-test
>  thread 0, count 12785417966
>  thread 1, count 12784090476
>  thread 2, count 12773373753
>  thread 3, count 12782542707
>  thread 4, count 12767835404
>  thread 5, count 12754382637
>  thread 6, count 12783454775
>  thread 7, count 12605966064
>  thread 8, count 12635255271
>  thread 9, count 12783436261
>
> Here's the test:
>
> $ cat /tmp/disp-step-buffers-test.c
> #include <pthread.h>
> #include <unistd.h>
> #include <stdlib.h>
> #include <stdio.h>
>
> #define NUM_THREADS 10
>
> static pthread_t child_thread[NUM_THREADS];
> static unsigned long long counters[NUM_THREADS];
> static volatile int done;
>
> static void *
> child_function (void *arg)
> {
>   while (!done)
>     counters[(long) arg]++;   // set breakpoint here
>   return NULL;
> }
>
> int
> main (void)
> {
>   long i;
>
>   for (i = 0; i < NUM_THREADS; i++)
>     pthread_create (&child_thread[i], NULL, child_function, (void *) i);
>
>   sleep (3);
>
>   done = 1;
>
>   for (i = 0; i < NUM_THREADS; i++)
>     pthread_join (child_thread[i], NULL);
>
>   for (i = 0; i < NUM_THREADS; i++)
>     printf ("thread %ld, count %llu\n", i, counters[i]);
>   return 0;
> }
>
> And here are the results under gdb.
>
> Test #1, pristine master (119e99bb7f50):
>
>  (gdb) b 16 if 0
>  Breakpoint 1 at 0x1290: file disp-step-buffers-test.c, line 16.
>  (gdb) r
>  Starting program: /tmp/disp-step-buffers-test
>  thread 0, count 1661
>  thread 1, count 1663
>  thread 2, count 1646
>  thread 3, count 1663
>  thread 4, count 1622
>  thread 5, count 1661
>  thread 6, count 1659
>  thread 7, count 1662
>  thread 8, count 1660
>  thread 9, count 1660
>  [Inferior 1 (process 18852) exited normally]
>  (gdb)
>
> Test #2, patches 1 through 11 applied, i.e., 1 buffer:
>
>  (gdb) b 16 if 0
>  Breakpoint 1 at 0x1290: file disp-step-buffers-test.c, line 16.
>  (gdb) r
>  Starting program: /tmp/disp-step-buffers-test
>  ...
>  thread 0, count 966
>  thread 1, count 950
>  thread 2, count 951
>  thread 3, count 950
>  thread 4, count 946
>  thread 5, count 948
>  thread 6, count 946
>  thread 7, count 979
>  thread 8, count 951
>  thread 9, count 966
>  [Inferior 1 (process 16099) exited normally]
>  (gdb)
>
> Test #3, patches 1 through 12 applied, i.e., 2 buffers:
>
>  (gdb) r
>  Starting program: /tmp/disp-step-buffers-test
>  ...
>  thread 0, count 1124
>  thread 1, count 1128
>  thread 2, count 1127
>  thread 3, count 1123
>  thread 4, count 1121
>  thread 5, count 1125
>  thread 6, count 1126
>  thread 7, count 1126
>  thread 8, count 1122
>  thread 9, count 1122
>  [Inferior 1 (process 14983) exited normally]
>
> Wow, test #2 was surprising, it's twice as slow as current
> master.  That was unexpected.
>
> Test #3 with the two buffers improves the speed a bit, but still
> quite behind current master.  I think this should be sorted out,
> at least understood.
>
> I checked, and it's patch #9 the one that introduces the slowdown:
>
>  gdb: move displaced stepping logic to gdbarch, allow starting concurrent displaced steps
>
> so, I'd suspect some new linear thread walks are causing this?
> E.g., displaced_step_in_progress.  Maybe we need to keep counters
> of number of threads displaced stepping instead of walking all threads,
> for example.  I didn't expect this to be observable with only 10 threads.
> I'd expect syscall time / ptrace, etc. to dominate.  Maybe it's
> something else?
>
> The above numbers were with an -O0 gdb.  I tried again against a gdb built
> at -O2, and the absolute numbers were of course better, but the
> slowdown is still observed.  I also tried with different numbers
> of threads.  Here's what I see:
>
> |threads | master | patch #11 | patch #12|
> |----------------------------------------|
> |     10 | 3959   |      2847 |      3055|
> |     20 | 1806   |      1055 |      1134|
> |     50 |  690   |       258 |       258|
>

I can reproduce those results, thanks for the benchmark progam.  I
previously tried to measure how much time it takes to have N threads do
M displaced steps. measuring the whole execution run time of GDB, but
that wasn't very precise.  Your method appears to give more reproducible
results.

I modified your program slightly such that it sums the counters for all
threads at the end and prints it.  I think that looking at the metric
"total number of displaced steps done in that period of time (across all
threads)" makes sense.

I focused on patch 9 and ignored 10-12, since it's patch 9 that is the main
culprit.  I made 4 experimental patches on top of patch 9, which are appended
at the bottom of this email (I think you can apply all of them with on big
git-am).  They are:

A. Implement displaced_step_in_progress without linear walk on thread list

   I followed your suggestion and added a counter of the number of
   displaced steps in progress in each inferior.  That allows
   implementing displaced_step_in_progress_any_thread and
   displaced_step_in_progress more efficiently thank walking all
   threads.

B. Append remaining threads to step in O(1)

   I also followed you suggestion and made it so we append remaining
   threads to step by fiddling with linked list pointers, in
   start_step_over.

C. Stop trying to prepare displaced steps for an inferior when no available buffers

   When the target tells us there is no buffer available for a thread,
   skip any subsequent thread of the same inferior for the rest of
   start_step_over.  This won't reduce the number of iterations in
   start_step_over, but it means we'll do much less unnecessary attempts
   at resuming threads when we know it won't work.

   I didn't include this in my original patchset, because this won't
   work if we want to allow arches to do buffer sharing.  It could
   return UNAVAILABLE for a thread, but then return OK for a subsequent
   thread, if that thread happens to be able to share a buffer with
   another thread displaced stepping the same PC.

   Perhaps we could have a way to ask arches "do you do buffer
   sharing?", and if not we can apply this optimization.  However, since
   I was planning on implementing buffer sharing as a follow-up, we'll
   be back in the same boat then...  But I still included this patch so
   you can see the speedup it provides.

D. Break loop on unavailable

   This is not correct either, but it shows that lots of time is lost in
   start_step_over.  It make start_step_over break out of the loop as
   soon as we get one unavailable.  If we had hindsight and could know
   that no thread further in the list will be able to initiate a
   displaced step, we could break out of the loop immediatly.  And this
   is the kind of speedup we'd get.

I ran the test with 100 threads to try to expose more the inefficiencies
and for a bit longer (20 seconds) to try to reduce the variations.

  master: 109,015
  #9:      20,304
  #9 + A:  19,807
  #9 + B:  20,499
  #9 + C:  66,468
  #9 + D: 103,406

Since patches C and D are about reducing the work in start_step_over, I
think it shows clearly that looping over all threads there is what gives
the biggest hit.  Before this patch, we break out as soon as we manage
to initiate a displaced step, whereas in patch 9 we constantly go
through all threads in the list

Patch C saves us the cost of all those unnecessary
switch_to_thread/keep_going_pass_signal.

Patch D saves us the cost of what is above that (mostly
thread_still_needs_step_over I guess), plus the actual iteration cost.

Simon


From 12f90dbe8522afef9c619766c403095f6adcac77 Mon Sep 17 00:00:00 2001
From: Simon Marchi <simon.marchi@polymtl.ca>
Date: Tue, 24 Nov 2020 22:12:55 -0500
Subject: [PATCH 1/4] Implement displaced_step_in_progress without linear walk
 on thread list

Keep a counter of the number of active displaced steps in each inferior.
This allows implementing displaced_step_in_progress and
displaced_step_in_progress_any_thread more efficiently.

Change-Id: If2609856f339055ad326652021e6ca911419bdc9
---
 gdb/displaced-stepping.h |  3 +++
 gdb/infrun.c             | 17 ++++++++---------
 2 files changed, 11 insertions(+), 9 deletions(-)

diff --git a/gdb/displaced-stepping.h b/gdb/displaced-stepping.h
index 6c1da46777c2..8de2b98bc7dc 100644
--- a/gdb/displaced-stepping.h
+++ b/gdb/displaced-stepping.h
@@ -100,11 +100,14 @@ struct displaced_step_inferior_state
   void reset ()
   {
     failed_before = false;
+    active_count = 0;
   }

   /* True if preparing a displaced step ever failed.  If so, we won't
      try displaced stepping for this inferior again.  */
   bool failed_before;
+
+  unsigned int active_count;
 };

 /* Per-thread displaced stepping state.  */
diff --git a/gdb/infrun.c b/gdb/infrun.c
index 521f4a65f0f7..f659de2a474f 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -1483,13 +1483,7 @@ displaced_step_in_progress_thread (thread_info *thread)
 static bool
 displaced_step_in_progress (inferior *inf)
 {
-  for (thread_info *thread : inf->non_exited_threads ())
-    {
-      if (displaced_step_in_progress_thread (thread))
-	return true;
-    }
-
-  return false;
+  return inf->displaced_step_state.active_count > 0;
 }

 /* Return true if any thread is doing a displaced step.  */
@@ -1497,9 +1491,9 @@ displaced_step_in_progress (inferior *inf)
 static bool
 displaced_step_in_progress_any_thread ()
 {
-  for (thread_info *thread : all_non_exited_threads ())
+  for (inferior *inf : all_non_exited_inferiors ())
     {
-      if (displaced_step_in_progress_thread (thread))
+      if (displaced_step_in_progress (inf))
 	return true;
     }

@@ -1709,6 +1703,8 @@ displaced_step_prepare_throw (thread_info *tp)
 			  paddress (gdbarch, original_pc),
 			  paddress (gdbarch, displaced_pc));

+  tp->inf->displaced_step_state.active_count++;
+
   return DISPLACED_STEP_PREPARE_STATUS_OK;
 }

@@ -1780,6 +1776,9 @@ displaced_step_finish (thread_info *event_thread, enum gdb_signal signal)

   displaced_step_reset_cleanup cleanup (displaced);

+  gdb_assert (event_thread->inf->displaced_step_state.active_count > 0);
+  event_thread->inf->displaced_step_state.active_count--;
+
   /* Do the fixup, and release the resources acquired to do the displaced
      step. */
   return gdbarch_displaced_step_finish (displaced->get_original_gdbarch (),
-- 
2.29.2


From 41d2e7ede90945888fd3b24c819d66a3d7b947ae Mon Sep 17 00:00:00 2001
From: Simon Marchi <simon.marchi@polymtl.ca>
Date: Tue, 24 Nov 2020 22:43:15 -0500
Subject: [PATCH 2/4] Append remaining threads to step in O(1)

Add a function to enqueue a whole chain to the global thread step over
chain.

Change-Id: Ifca37af2006ce132348aa66c97294c13bcd09bf4
---
 gdb/gdbthread.h |  2 ++
 gdb/infrun.c    | 12 +-----------
 gdb/thread.c    | 22 ++++++++++++++++++++++
 3 files changed, 25 insertions(+), 11 deletions(-)

diff --git a/gdb/gdbthread.h b/gdb/gdbthread.h
index 71dc14711be5..cbaee420546e 100644
--- a/gdb/gdbthread.h
+++ b/gdb/gdbthread.h
@@ -748,6 +748,8 @@ extern bool value_in_thread_stack_temporaries (struct value *,
 /* Add TP to the end of the global pending step-over chain.  */

 extern void global_thread_step_over_chain_enqueue (struct thread_info *tp);
+extern void global_thread_step_over_chain_enqueue_chain
+  (thread_info *chain_head);

 /* Remove TP from step-over chain LIST_P.  */

diff --git a/gdb/infrun.c b/gdb/infrun.c
index f659de2a474f..4c6a06d053e3 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -1954,17 +1954,7 @@ start_step_over (void)
 	infrun_debug_printf ("putting back %d threads to step in global queue",
 			     thread_step_over_chain_length (threads_to_step));

-	while (threads_to_step != nullptr)
-	  {
-	    thread_info *thread = threads_to_step;
-
-	    /* Remove from that list.  */
-	    thread_step_over_chain_remove (&threads_to_step, thread);
-
-	    /* Add to global list.  */
-	    global_thread_step_over_chain_enqueue (thread);
-
-	  }
+	global_thread_step_over_chain_enqueue_chain (threads_to_step);
       }

   return started;
diff --git a/gdb/thread.c b/gdb/thread.c
index a3c9aed1754a..d82e67732812 100644
--- a/gdb/thread.c
+++ b/gdb/thread.c
@@ -442,6 +442,28 @@ global_thread_step_over_chain_enqueue (struct thread_info *tp)

 /* See gdbthread.h.  */

+void
+global_thread_step_over_chain_enqueue_chain (thread_info *chain_head)
+{
+  gdb_assert (chain_head->step_over_next != nullptr);
+  gdb_assert (chain_head->step_over_prev != nullptr);
+
+  if (global_thread_step_over_chain_head == nullptr)
+    global_thread_step_over_chain_head = chain_head;
+  else
+    {
+      thread_info *global_last = global_thread_step_over_chain_head->step_over_prev;
+      thread_info *chain_last = chain_head->step_over_prev;
+
+      chain_last->step_over_next = global_thread_step_over_chain_head;
+      global_last->step_over_next = chain_head;
+      global_thread_step_over_chain_head->step_over_prev = chain_last;
+      chain_head->step_over_prev = global_last;
+    }
+}
+
+/* See gdbthread.h.  */
+
 void
 global_thread_step_over_chain_remove (struct thread_info *tp)
 {
-- 
2.29.2


From 5ebafa794f3f74dcfe9a412bc58cab2a2c5d04d4 Mon Sep 17 00:00:00 2001
From: Simon Marchi <simon.marchi@efficios.com>
Date: Mon, 16 Mar 2020 17:21:55 -0400
Subject: [PATCH 3/4] Stop trying to prepare displaced steps for an inferior
 when no available buffers

Once we got one _UNAVAILABLE for a given inferior, don't try to prepare
other threads for displaced stepping for that inferior during this
execution of start_step_over.

Change-Id: I13063e3c21729f7c7556c3ede38069a39ee98f1c
---
 gdb/displaced-stepping.h |  2 ++
 gdb/infrun.c             | 10 ++++++++++
 2 files changed, 12 insertions(+)

diff --git a/gdb/displaced-stepping.h b/gdb/displaced-stepping.h
index 8de2b98bc7dc..001260c6d921 100644
--- a/gdb/displaced-stepping.h
+++ b/gdb/displaced-stepping.h
@@ -108,6 +108,8 @@ struct displaced_step_inferior_state
   bool failed_before;

   unsigned int active_count;
+
+  bool unavailable = false;
 };

 /* Per-thread displaced stepping state.  */
diff --git a/gdb/infrun.c b/gdb/infrun.c
index 4c6a06d053e3..e05bb95347e8 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -1687,6 +1687,7 @@ displaced_step_prepare_throw (thread_info *tp)
 			      target_pid_to_str (tp->ptid).c_str ());

       global_thread_step_over_chain_enqueue (tp);
+      tp->inf->displaced_step_state.unavailable = true;

       return DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE;
     }
@@ -1845,6 +1846,9 @@ start_step_over (void)
   infrun_debug_printf ("stealing global queue of threads to step, length = %d",
 		       thread_step_over_chain_length (threads_to_step));

+  for (inferior *inf : all_inferiors ())
+    inf->displaced_step_state.unavailable = false;
+
   for (thread_info *tp = threads_to_step; tp != NULL; tp = next)
     {
       struct execution_control_state ecss;
@@ -1894,6 +1898,12 @@ start_step_over (void)
       if (!target_is_non_stop_p () && !step_what)
 	continue;

+      if (tp->inf->displaced_step_state.unavailable)
+	{
+	  global_thread_step_over_chain_enqueue (tp);
+	  continue;
+	}
+
       switch_to_thread (tp);
       reset_ecs (ecs, tp);
       keep_going_pass_signal (ecs);
-- 
2.29.2


From b941a5e270602579e6c22569db8281f3aba6953d Mon Sep 17 00:00:00 2001
From: Simon Marchi <simon.marchi@polymtl.ca>
Date: Tue, 24 Nov 2020 23:21:07 -0500
Subject: [PATCH 4/4] Break loop on unavailable

This gives a preview of how it could be if we were able to break out of
the loop once we know no more displaced steps prepare are going to
succeed.

Change-Id: I7a22acfbbbd4673367f3f93fb946cbc529d8f028
---
 gdb/infrun.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/gdb/infrun.c b/gdb/infrun.c
index e05bb95347e8..4c96cdf0685c 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -1901,7 +1901,7 @@ start_step_over (void)
       if (tp->inf->displaced_step_state.unavailable)
 	{
 	  global_thread_step_over_chain_enqueue (tp);
-	  continue;
+	  break;
 	}

       switch_to_thread (tp);
-- 
2.29.2


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

* Re: [PATCH 03/12] gdb: rename things related to step over chains
  2020-11-25  1:28   ` Pedro Alves
@ 2020-11-25 13:16     ` Simon Marchi
  0 siblings, 0 replies; 44+ messages in thread
From: Simon Marchi @ 2020-11-25 13:16 UTC (permalink / raw)
  To: Pedro Alves, gdb-patches

On 2020-11-24 8:28 p.m., Pedro Alves wrote:
>> -extern struct thread_info *thread_step_over_chain_next (struct thread_info *tp);
>> +extern struct thread_info *global_thread_step_over_chain_next (struct thread_info *tp);
>
> Line too long now.

Fixed (by removing struct keywords).

>> --- a/gdb/thread.c
>> +++ b/gdb/thread.c
>> @@ -207,7 +207,7 @@ set_thread_exited (thread_info *tp, bool silent)
>>  {
>>    /* Dead threads don't need to step-over.  Remove from queue.  */
>
> "Remove from chain", I guess.

Fixed.

Thanks,

Simon

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

* Re: [PATCH 06/12] gdb: introduce status enum for displaced step prepare/finish
  2020-11-11 23:36   ` Andrew Burgess
@ 2020-11-25 13:17     ` Simon Marchi
  0 siblings, 0 replies; 44+ messages in thread
From: Simon Marchi @ 2020-11-25 13:17 UTC (permalink / raw)
  To: Andrew Burgess; +Cc: gdb-patches

On 2020-11-11 6:36 p.m., Andrew Burgess wrote:
> I looked though all the patches up to and including this one.  I don't
> have too much knowledge of the displaced stepping mechanism, but so
> far everything seemed good.
>
> Thanks,
> Andrew

Ok, thanks for the review!

Simon

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

* Re: [PATCH 06/12] gdb: introduce status enum for displaced step prepare/finish
  2020-11-25  1:30   ` Pedro Alves
@ 2020-11-25 13:20     ` Simon Marchi
  0 siblings, 0 replies; 44+ messages in thread
From: Simon Marchi @ 2020-11-25 13:20 UTC (permalink / raw)
  To: Pedro Alves, gdb-patches

On 2020-11-24 8:30 p.m., Pedro Alves wrote:
> On 11/10/20 9:46 PM, Simon Marchi via Gdb-patches wrote:
>> +enum displaced_step_finish_status
>> +{
>> +  /* Either the instruction was stepped and fixed up, or the specified thread
>> +     wasn't executing a displaced step (in which case there's nothing to
>> +     finish). */
>> +  DISPLACED_STEP_FINISH_STATUS_OK,
>> +
>> +  /* The thread was executing a displaced step, but didn't complete it.  */
>> +  DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED,
>
> There's an ambiguity here that gave me pause -- the enumeration is
> called "not executed", but the comment says "was executing".  I'd suggest:
>
>   /* The thread started a displaced step, but didn't complete it.  */

Fixed.

>> -/* If we displaced stepped an instruction successfully, adjust
>> -   registers and memory to yield the same effect the instruction would
>> -   have had if we had executed it at its original address, and return
>> -   1.  If the instruction didn't complete, relocate the PC and return
>> -   -1.  If the thread wasn't displaced stepping, return 0.  */
>> +/* If we displaced stepped an instruction successfully, adjust registers and
>> +   memory to yield the same effect the instruction would have had if we had
>> +   executed it at its original address, and return
>> +   DISPLACED_STEP_PREPARE_STATUS_OK.  If the instruction didn't complete,
>> +   relocate the PC and return DISPLACED_STEP_PREPARE_STATUS_NOT_EXECUTED.
>
> DISPLACED_STEP_PREPARE_... -> DISPLACED_STEP_FINISH_...

Fixed.

>
>> -static int
>> +   If the thread wasn't displaced stepping, return
>> +   DISPLACED_STEP_PREPARE_STATUS_OK as well..  */
>
> Here too.  Also, spurious double period "..".

Fixed.

Thanks,

Simon

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

* Re: [PATCH 09/12] gdb: move displaced stepping logic to gdbarch, allow starting concurrent displaced steps
  2020-11-25  1:40   ` Pedro Alves
@ 2020-11-25 19:29     ` Simon Marchi
  2020-11-25 19:35       ` Simon Marchi
  2020-11-26 14:24       ` Pedro Alves
  0 siblings, 2 replies; 44+ messages in thread
From: Simon Marchi @ 2020-11-25 19:29 UTC (permalink / raw)
  To: Pedro Alves, gdb-patches

On 2020-11-24 8:40 p.m., Pedro Alves wrote:
> On 11/10/20 9:46 PM, Simon Marchi via Gdb-patches wrote:
>
>> - Ask the gdbarch for the displaced step buffer location
>> - Save the existing bytes in the displaced step buffer
>> - Ask the gdbarch to copy the instruction into the displaced step buffer
>> - Set the pc of the thread to the beginning of the displaced step buffer
>>
>> Similarly, the "fixup" phase, executed after the instruction was
>> successfully single-stepped, is driven by the infrun code (function
>> displaced_step_fixup).  The steps are roughly:
>
> displaced_step_fixup -> displaced_step_finish.  And maybe "finish" phase?

Fixed.

>>
>> - Restore the original bytes in the displaced stepping buffer
>> - Ask the gdbarch to fixup the instruction result (adjust the target's
>>   registers or memory to do as if the instruction had been executed in
>>   its original location)
>>
>> The displaced_step_inferior_state::step_thread field indicates which
>> thread (if any) is currently using the displaced stepping buffer, so it
>> is used by displaced_step_prepare_throw to check if the displaced
>> stepping buffer is free to use or not.
>>
>> This patch defers the whole task of preparing and cleaning up after a
>> displaced step to the gdbarch.  Two new main gdbarch methods are added,
>> with the following sematics:
>
> "sematics" -> "semantics"

Fixed.

>>
>>   - gdbarch_displaced_step_prepare: Prepare for the given thread to
>>     execute a displaced step of the instruction located at its current PC.
>>     Upon return, everything should be ready for GDB to resume the thread
>>     (with either a single step or continue, as indicated by
>>     gdbarch_displaced_step_hw_singlestep) to make it displaced step the
>>     instruction.
>>
>>   - gdbarch_displaced_step_finish: Called when the thread stopped after
>>     having started a displaced step.  Verify if the instruction was
>>     executed, if so apply any fixup required to compensate for the fact
>>     that the instruction was executed at different place than its original
>
> "at a different place"

Fixed.

>>     pc.  Release any resources that was allocated for this displaced step.
>
> "that were allocated"

Fixed.

>
>>     Upon return, everything should be ready for GDB to resume the
>>     thread in its "normal" code path.
>>
>> The displaced_step_prepare_throw function now pretty much just offloads
>> to gdbarch_displaced_step_prepare and the displaced_step_finish function
>> offloads to gdbarch_displaced_step_finish.
>>
>> The gdbarch_displaced_step_location method is now unnecessary, so is
>> removed.  Indeed, the core of GDB doesn't know how many displaced step
>> buffers there are nor where they are.
>>
>> To keep the existing behavior for existing architectures, the logic that
>> was previously implemented in infrun.c for preparing and finishing a
>> displaced step is moved to displaced-stepping.c, to the
>> displaced_step_buffer class.  Architectures are modified to implement
>> the new gdbarch methods using this class.  The behavior is not expeicted
>> to change.
>
> "expeicted" -> "expected"

Fixed.

>> +
>> +  m_original_pc = regcache_read_pc (regcache);
>> +  displaced_pc = m_addr;
>> +
>> +  /* Save the original contents of the displaced stepping buffer.  */
>> +  m_saved_copy.resize (len);
>> +
>> +  int status = target_read_memory (m_addr, m_saved_copy.data (), len);
>> +  if (status != 0)
>> +    throw_error (MEMORY_ERROR,
>> +		 _("Error accessing memory address %s (%s) for "
>> +		   "displaced-stepping scratch space."),
>> +		 paddress (arch, m_addr), safe_strerror (status));
>> +
>> +  displaced_debug_printf ("saved %s: %s",
>> +			  paddress (arch, m_addr),
>> +			  displaced_step_dump_bytes
>> +			    (m_saved_copy.data (), len).c_str ());
>> +
>> +  m_copy_insn_closure = gdbarch_displaced_step_copy_insn (arch,
>> +							  m_original_pc,
>> +							  m_addr,
>> +							  regcache);
>> +  if (m_copy_insn_closure == nullptr)
>> +    {
>> +      /* The architecture doesn't know how or want to displaced step
>> +        this instruction or instruction sequence.  Fallback to
>> +        stepping over the breakpoint in-line.  */
>> +      return DISPLACED_STEP_PREPARE_STATUS_ERROR;
>> +    }
>> +
>> +  try
>> +    {
>> +      /* Resume execution at the copy.  */
>> +      regcache_write_pc (regcache, m_addr);
>> +    }
>> +  catch (...)
>
> I'd rather we don't use 'catch (...)' throughout gdb.  It swallows
> too much.  We should catch gdb_exception_error in most cases or
> gdb_exception when we also want to catch Ctrl-C/QUIT.  If something
> throws something else, I'd rather not swallow it, since it's most
> probably a bug.
>
> I'm a bit confused on error handling here.  Before the patch, we used
> displaced_step_reset_cleanup, but didn't swallow the error.  After the patch,
> the exception is swallowed and DISPLACED_STEP_PREPARE_STATUS_ERROR is
> returned.  Was that on purpose?

Hmm, I probably did this to ensure that any error would be reported by
returning a status code, rather than some errors returning _ERROR and
some errors throwing an exception.  So just for consistency.

Would it make sense to remove the _ERROR status code and report all
errors using exceptions then?

>> +/* Manage access to a single displaced stepping buffer.  */
>> +
>> +struct displaced_step_buffer
>> +{
>> +  displaced_step_buffer (CORE_ADDR buffer_addr)
>
> explicit.

Fixed.

>> +    : m_addr (buffer_addr)
>> +  {}
>> +
>> +  displaced_step_prepare_status prepare (thread_info *thread,
>> +					 CORE_ADDR &displaced_pc);
>> +
>> +  displaced_step_finish_status finish (gdbarch *arch, thread_info *thread,
>> +				       gdb_signal sig);
>> +
>> +  const displaced_step_copy_insn_closure *
>> +    copy_insn_closure_by_addr (CORE_ADDR addr);
>>
>> -  /* If this is not nullptr, this is the thread carrying out a
>> -     displaced single-step in process PID.  This thread's state will
>> -     require fixing up once it has completed its step.  */
>> -  thread_info *step_thread;
>> +  void restore_in_ptid (ptid_t ptid);
>>
>> -  /* The architecture the thread had when we stepped it.  */
>> -  gdbarch *step_gdbarch;
>> +private:
>>
>> -  /* The closure provided gdbarch_displaced_step_copy_insn, to be used
>> -     for post-step cleanup.  */
>> -  displaced_step_copy_insn_closure_up step_closure;
>> +  CORE_ADDR m_original_pc = 0;
>> +  const CORE_ADDR m_addr;
>>
>> -  /* The address of the original instruction, and the copy we
>> -     made.  */
>> -  CORE_ADDR step_original, step_copy;
>> +  /* If set, the thread currently using the buffer.  */
>> +  thread_info *m_current_thread = nullptr;
>>
>> -  /* Saved contents of copy area.  */
>> -  gdb::byte_vector step_saved_copy;
>> +  gdb::byte_vector m_saved_copy;
>> +  displaced_step_copy_insn_closure_up m_copy_insn_closure;
>
> Missing comments?

Fixed.

>> @@ -1554,9 +1549,9 @@ show_can_use_displaced_stepping (struct ui_file *file, int from_tty,
>>  static bool
>>  gdbarch_supports_displaced_stepping (gdbarch *arch)
>>  {
>> -  /* Only check for the presence of step_copy_insn.  Other required methods
>> -     are checked by the gdbarch validation.  */
>> -  return gdbarch_displaced_step_copy_insn_p (arch);
>> +  /* Only check for the presence of `prepare`.  `finish` is required by the
>> +     gdbarch verification to be provided if `prepare` is provided.  */
>
> This reads a little funny to me.  I'd suggest:
>
>   /* Only check for the presence of `prepare`.  The gdbarch verification ensures
>      that if `prepare` is provided, so is `finish`.  */

Fixed to use that.

>> +  return gdbarch_displaced_step_prepare_p (arch);
>>  }
>>
>>  /* Return non-zero if displaced stepping can/should be used to step
>
>> @@ -1669,96 +1662,52 @@ displaced_step_prepare_throw (thread_info *tp)
>>       jump/branch).  */
>>    tp->control.may_range_step = 0;
>>
>> -  /* We have to displaced step one thread at a time, as we only have
>> -     access to a single scratch space per inferior.  */
>> -
>> -  displaced_step_inferior_state *displaced
>> -    = get_displaced_stepping_state (tp->inf);
>> -
>> -  if (displaced->step_thread != nullptr)
>> -    {
>> -      /* Already waiting for a displaced step to finish.  Defer this
>> -	 request and place in queue.  */
>> -
>> -      displaced_debug_printf ("deferring step of %s",
>> -			      target_pid_to_str (tp->ptid).c_str ());
>> -
>> -      global_thread_step_over_chain_enqueue (tp);
>> -      return DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE;
>> -    }
>> -  else
>> -    displaced_debug_printf ("stepping %s now",
>> -			    target_pid_to_str (tp->ptid).c_str ());
>> -
>> -  displaced_step_reset (displaced);
>> +  /* We are about to start a displaced step for this thread.  If one is already
>> +     in progress, something's wrong..  */
>
> Double period.

Fixed.

>> +  gdb_assert (!disp_step_thread_state->in_progress ());
>>
>>    scoped_restore_current_thread restore_thread;
>>
>>    switch_to_thread (tp);
>>
>> -  original = regcache_read_pc (regcache);
>> +  CORE_ADDR original_pc = regcache_read_pc (regcache);
>> +  CORE_ADDR displaced_pc;
>>
>> -  copy = gdbarch_displaced_step_location (gdbarch);
>> -  len = gdbarch_max_insn_length (gdbarch);
>> +  displaced_step_prepare_status status =
>> +    gdbarch_displaced_step_prepare (gdbarch, tp, displaced_pc);
>
> " = " on the next line.

Fixed.

>> @@ -1934,14 +1831,22 @@ static step_over_what thread_still_needs_step_over (struct thread_info *tp);
>>  static bool
>>  start_step_over (void)
>>  {
>> -  struct thread_info *tp, *next;
>> +  thread_info *next;
>> +  bool started = false;
>>
>>    /* Don't start a new step-over if we already have an in-line
>>       step-over operation ongoing.  */
>>    if (step_over_info_valid_p ())
>> -    return false;
>> +    return started;
>
> I'd move the "bool started = false;" below this if line and continue
> writing explicit "return false" here, as it's less indirection.

Done.

>
>> +
>> +  /* Steal the global thread step over chain.  */
>
> It would be good expand the command explaining _why_ steal.

How about:

  /* Steal the global thread step over chain.  As we try to initiate displaced
     steps, threads will be enqueued in the global chain if no buffers are
     available.  If we iterated on the global chain directly, we might\x13 iterate
     indefinitely.  */

>> @@ -2033,7 +1945,30 @@ start_step_over (void)
>>  	 displaced step on a thread of other process. */
>>      }
>>
>> -  return false;
>> +    /* If there are threads left in the THREADS_TO_STEP list, but we have
>> +       detected that we can't start anything more, put back these threads
>> +       in the global list.  */
>
> Do we also need to do this if an exception happens to escape the function?
> We might end up pretty bonkers anyhow if that happens, though...

Yes, I realized that when playing with this code again yesterday.  A
scope_exit would help for this I think.

But I'm also wondering if we should enqueue back the thread TP that was
dequeued, that caused an error.  This one has already been dequeued from
THREADS_TO_STEP.  For example, if thread_still_needs_step_over throws
(it reads the PC, so that could happen), should we put back the thread
in the global chain?  If we do, and the same error happens over and
over, the thread will never leave the step over chain and be the first
in line, preventing any other displaced step to happen.

If, on the other hand, the error was just an intermittent one and we
don't put it back in the queue, the next time we'll resume the thread
we'll realize it needs a step over and enqueue it again.  So it sounds
less risky to me to just not enqueue back the thread on error.  I'm
really not sure, I find it difficult to reason about all the possible
cases.

Also, re-reading that code, I noticed that we only return "true" when an
inline step over is started, or if a displaced step on an all-stop
target was started.  That's the case even before the patch.  I suppose
that's on purpose, because the caller wants to know whether it can
resume more stuff or not?  If so, I think we should update
start_step_over's documentation:

  /* Are there any pending step-over requests?  If so, run all we can
     now and return true.  Otherwise, return false.  */

>> +    if (threads_to_step == NULL)
>> +      infrun_debug_printf ("step-over queue now empty");
>> +    else
>> +      {
>> +	infrun_debug_printf ("putting back %d threads to step in global queue",
>> +			     thread_step_over_chain_length (threads_to_step));
>> +
>> +	while (threads_to_step != nullptr)
>> +	  {
>> +	    thread_info *thread = threads_to_step;
>> +
>> +	    /* Remove from that list.  */
>> +	    thread_step_over_chain_remove (&threads_to_step, thread);
>> +
>> +	    /* Add to global list.  */
>> +	    global_thread_step_over_chain_enqueue (thread);
>> +
>
> Spurious empty line.  But, did you look into splicing the whole threads_to_step
> chain into the global chain in O(1), with just some prev/next pointer
> adjustments?

Fixed that in an experimental patch for now, but it would make sense to
do so, so I'll probably end up folding it into this one.

>> +	  }
>> +      }
>> +
>> +  return started;
>>  }
>>
>>  /* Update global variables holding ptids to hold NEW_PTID if they were
>> @@ -3618,18 +3553,16 @@ prepare_for_detach (void)
>>    struct inferior *inf = current_inferior ();
>>    ptid_t pid_ptid = ptid_t (inf->pid);
>>
>> -  displaced_step_inferior_state *displaced = get_displaced_stepping_state (inf);
>> -
>>    /* Is any thread of this process displaced stepping?  If not,
>>       there's nothing else to do.  */
>> -  if (displaced->step_thread == nullptr)
>> +  if (displaced_step_in_progress (inf))
>>      return;
>>
>>    infrun_debug_printf ("displaced-stepping in-process while detaching");
>>
>>    scoped_restore restore_detaching = make_scoped_restore (&inf->detaching, true);
>>
>> -  while (displaced->step_thread != nullptr)
>> +  while (displaced_step_in_progress (inf))
>>      {
>>        struct execution_control_state ecss;
>>        struct execution_control_state *ecs;
>> @@ -5293,25 +5226,23 @@ handle_inferior_event (struct execution_control_state *ecs)
>>        {
>>  	struct regcache *regcache = get_thread_regcache (ecs->event_thread);
>>  	struct gdbarch *gdbarch = regcache->arch ();
>> +	inferior *parent_inf = find_inferior_ptid (ecs->target, ecs->ptid);
>> +
>> +	if (ecs->ws.kind == TARGET_WAITKIND_FORKED)
>> +	  {
>> +	    /* Restore in the child process any displaced stepping buffers that
>> +	       were in use at the time of the fork.  */
>> +	    gdbarch_displaced_step_restore_all_in_ptid
>> +	      (gdbarch, parent_inf, ecs->ws.value.related_pid);
>> +	  }
>
> Why was this moved out of the displaced_step_in_progress_thread check
> below?

If:

1. thread 1 is doing a displaced step
2. thread 2 does a fork, not letting time for thread 1 to complete

event_thread is thread 2, and it is not doing a displaced step, so we
don't enter the

  if (displaced_step_in_progress_thread (ecs->event_thread))

But we still want to restore the bytes in the child used as the
displaced stepping buffer for thread 1.  Does that make sense?  Is it
possible that the current code is not correct in that regard?

>> @@ -2530,6 +2533,61 @@ linux_displaced_step_location (struct gdbarch *gdbarch)
>>    return addr;
>>  }
>>
>> +/* Implementation gdbarch_displaced_step_prepare.  */
>
> "Implementation of" ?

Changed for

  /* See linux-tdep.h.  */

>
>> +
>> +displaced_step_prepare_status
>> +linux_displaced_step_prepare (gdbarch *arch, thread_info *thread,
>> +			      CORE_ADDR &displaced_pc)
>> +{
>> +  linux_info *per_inferior = get_linux_inferior_data (thread->inf);
>> +
>> +  if (!per_inferior->disp_step_buf.has_value ())
>> +    {
>> +      CORE_ADDR disp_step_buf_addr
>> +	= linux_displaced_step_location (thread->inf->gdbarch);
>> +
>> +      per_inferior->disp_step_buf.emplace (disp_step_buf_addr);
>> +    }
>> +
>> +  return per_inferior->disp_step_buf->prepare (thread, displaced_pc);
>> +}
>> +
>> +/* Implementation gdbarch_displaced_step_finish.  */
>
> Ditto.

Changed for "See...".

>
>> +
>> +displaced_step_finish_status
>> +linux_displaced_step_finish (gdbarch *arch, thread_info *thread, gdb_signal sig)
>> +{
>> +  linux_info *per_inferior = get_linux_inferior_data (thread->inf);
>> +
>> +  gdb_assert (per_inferior->disp_step_buf.has_value ());
>> +
>> +  return per_inferior->disp_step_buf->finish (arch, thread, sig);
>> +}
>> +
>> +const displaced_step_copy_insn_closure *
>> +linux_displaced_step_copy_insn_closure_by_addr (inferior *inf, CORE_ADDR addr)
>
> Ditto on the immaginary comment.  :-)

Added "See...".

>
>> +{
>> +  linux_info *per_inferior = linux_inferior_data.get (inf);
>> +
>> +  if (per_inferior == nullptr
>> +      || !per_inferior->disp_step_buf.has_value ())
>> +    return nullptr;
>> +
>> +  return per_inferior->disp_step_buf->copy_insn_closure_by_addr (addr);
>> +}
>> +
>> +void
>> +linux_displaced_step_restore_all_in_ptid (inferior *parent_inf, ptid_t ptid)
>> +{
>> +  linux_info *per_inferior = linux_inferior_data.get (parent_inf);
>> +

Same here.

>>  /* See gdbthread.h.  */
>> @@ -403,9 +411,32 @@ thread_is_in_step_over_chain (struct thread_info *tp)
>>
>>  /* See gdbthread.h.  */
>>
>> +int
>> +thread_step_over_chain_length (thread_info *tp)
>> +{
>> +  if (tp == nullptr)
>> +    return 0;
>> +
>> +  int num = 1;
>
> Should we add:
>
>   gdb_assert (thread_is_in_step_over_chain (tp));
>
> ?
>
> But then again, it's a bit odd to allow tp == nullptr,
> but not allow tp->step_over_next == nullptr?

Well, a pointer to an empty thread step over chain is a nullptr: when
the global chain is empty, global_thread_step_over_chain is nullptr.
Same for threads_to_step that is local to start_step_over.  So
it makes sense that we can do thread_step_over_chain_length(nullptr).

However, it would be a mistake to pass a non-nullptr pointer to a thread
that is not in a step over chain, so I think it would be a good idea to
add the assert you propose.

I will clarify the documentation of the function:

  /* Return the length of the the step over chain TP is in.

     If TP is non-nullptr, the thread must be in a step over chain.
     TP may be nullptr, in which case it denotes an empty list, so a length of
     0.  */

>> +  thread_info *iter = tp->step_over_next;
>> +
>> +  while (iter != tp)
>> +    {
>> +      num++;
>> +      iter = iter->step_over_next;
>> +    }
>
> This seems to be begging for a "for":
>
>   int num = 1;
>   for (thread_info *iter = tp->step_over_next;
>        iter != tp;
>        iter = iter->step_over_next)
>     num++;

True, changed for:

  for (thread_info *iter = tp->step_over_next; iter != tp; iter->step_over_next)
    ++num;

Simon

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

* Re: [PATCH 09/12] gdb: move displaced stepping logic to gdbarch, allow starting concurrent displaced steps
  2020-11-25 19:29     ` Simon Marchi
@ 2020-11-25 19:35       ` Simon Marchi
  2020-11-26 14:25         ` Pedro Alves
  2020-11-26 14:24       ` Pedro Alves
  1 sibling, 1 reply; 44+ messages in thread
From: Simon Marchi @ 2020-11-25 19:35 UTC (permalink / raw)
  To: Simon Marchi, Pedro Alves, gdb-patches

On 2020-11-25 2:29 p.m., Simon Marchi via Gdb-patches wrote:
> True, changed for:
>
>   for (thread_info *iter = tp->step_over_next; iter != tp; iter->step_over_next)
>     ++num;

Good thing the compiler didn't let me compile that, it would have made
an infinite loop!  Here's the correct one:

  for (thread_info *iter = tp->step_over_next; iter != tp;
       iter = iter->step_over_next)
    ++num;

Simon

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

* Re: [PATCH 12/12] gdb: use two displaced step buffers on amd64/Linux
  2020-11-25  6:26     ` Simon Marchi
@ 2020-11-25 20:07       ` Simon Marchi
  2020-11-25 20:56         ` Simon Marchi
  0 siblings, 1 reply; 44+ messages in thread
From: Simon Marchi @ 2020-11-25 20:07 UTC (permalink / raw)
  To: Pedro Alves, Simon Marchi, gdb-patches

On 2020-11-25 1:26 a.m., Simon Marchi wrote:
> Since patches C and D are about reducing the work in start_step_over, I
> think it shows clearly that looping over all threads there is what gives
> the biggest hit.  Before this patch, we break out as soon as we manage
> to initiate a displaced step, whereas in patch 9 we constantly go
> through all threads in the list

Sorry, that's incorrect.  Before this patch, when a displaced step is
already started in an inferior, we skip any subsequent thread for that
inferior.  It's done very early, before calling
thread_still_needs_step_over.

If I modify patch C to do the same, it looks more like this:

(And to clarify my previous mail, it wasn't clear when I said "#9 + A",
"9 + B" and so on.  It should say "#9 + A", "#9 + A + B", and so on.
Otherwise it gives the impression I tested the fixup patches
individually, which I didn't.)

master:             109,362
#9:                  19,815
#9 + A:              19,463
#9 + A + B:          20,152
#9 + A + B + C:     101,170
#9 + A + B + C + D: 103,948

And for fun, with two buffers:

#9 + A + B + C + D + #10 to #12: 105,864

So, with those mitigations (especially patch C), it's not as bad as it
was, but still slower than master.  And even when using 2 buffers, which
is meant to speed things up, so it's not good.

So, patch C makes implements that when a displaced step prepare returns
"unavailable" for an inferior, we don't try to start any more for that
inferior (for the rest of that start_step_over call).  That
unfortunately does not allow to implement "perfect" displaced step
buffer sharing though.

Maybe we can settle for a compromise though, since sharing buffers is
just an optimization and not required: implement something like patch C,
but also implement buffer sharing for threads that need to step the same
PC.  If the threads are ordered in the chain in a lucky way, in a way
that multiple threads at the same PC are handled before the buffers are
all occupied, then these threads will share a buffer.  Once all buffers
are occupied, we quit, even if there might more threads able to share a
buffer further in the list.

To illustrate, let's say you have these threads in the step over chain,
that require to step over PC A, B and C:

  T1 at PC A
  T2 at PC A
  T3 at PC B
  T4 at PC A
  T5 at PC C
  T6 at PC A

With two buffers, we'll be able to accomodate the first 4 threads.  When
we try to prepare the disp step for T5, the implemention will return
"unavailable", so T6 won't even be considered.  So in the end T1, T2 and
T4 will share a buffer whle T3 will be by itself in the other buffer.

I think that strikes a good balance because as long as we didn't get an
"unavailable", we do expensive work to try to prepare some displaced
steps, but it's useful work since we'll actually resume these threads.
But after that, we plow our way through a list of hundreds of threads,
doing expensive work for each, with the hope of finding some for which
the prepare may work.  That's not a very good investment (well, it very
much depends on the workload).

We still have to figure out why the performance regresses compared to
master.  One thing is that we still do an extra resume attempt each
time.  We do one resume where the disp step prepare succeeds and one
more where it fails.  That's twice as much work as before, where we did
one successful prepare and then skipped the next ones.  I'll make an
experimental change such that the arch can say "the prepare worked, but
now I'm out of buffer", just to see how that improves (or not)
performance.

But I'm hitting send on this email now, so that you don't spend too much
time rebuking the lies of my previous email :).

Simon

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

* Re: [PATCH 12/12] gdb: use two displaced step buffers on amd64/Linux
  2020-11-25 20:07       ` Simon Marchi
@ 2020-11-25 20:56         ` Simon Marchi
  2020-11-26 21:43           ` Simon Marchi
  0 siblings, 1 reply; 44+ messages in thread
From: Simon Marchi @ 2020-11-25 20:56 UTC (permalink / raw)
  To: Pedro Alves, Simon Marchi, gdb-patches

On 2020-11-25 3:07 p.m., Simon Marchi wrote:
> We still have to figure out why the performance regresses compared to
> master.  One thing is that we still do an extra resume attempt each
> time.  We do one resume where the disp step prepare succeeds and one
> more where it fails.  That's twice as much work as before, where we did
> one successful prepare and then skipped the next ones.  I'll make an
> experimental change such that the arch can say "the prepare worked, but
> now I'm out of buffer", just to see how that improves (or not)
> performance.

That made just a small difference.  Let's call this new patch E:

 #9 + A + B + C + D + E: 105,727

Simon

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

* Re: [PATCH 09/12] gdb: move displaced stepping logic to gdbarch, allow starting concurrent displaced steps
  2020-11-25 19:29     ` Simon Marchi
  2020-11-25 19:35       ` Simon Marchi
@ 2020-11-26 14:24       ` Pedro Alves
  2020-11-30 20:26         ` Simon Marchi
  1 sibling, 1 reply; 44+ messages in thread
From: Pedro Alves @ 2020-11-26 14:24 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches

On 11/25/20 7:29 PM, Simon Marchi wrote:
> On 2020-11-24 8:40 p.m., Pedro Alves wrote:
> 
>>> +
>>> +  m_original_pc = regcache_read_pc (regcache);
>>> +  displaced_pc = m_addr;
>>> +
>>> +  /* Save the original contents of the displaced stepping buffer.  */
>>> +  m_saved_copy.resize (len);
>>> +
>>> +  int status = target_read_memory (m_addr, m_saved_copy.data (), len);
>>> +  if (status != 0)
>>> +    throw_error (MEMORY_ERROR,
>>> +		 _("Error accessing memory address %s (%s) for "
>>> +		   "displaced-stepping scratch space."),
>>> +		 paddress (arch, m_addr), safe_strerror (status));
>>> +
>>> +  displaced_debug_printf ("saved %s: %s",
>>> +			  paddress (arch, m_addr),
>>> +			  displaced_step_dump_bytes
>>> +			    (m_saved_copy.data (), len).c_str ());
>>> +
>>> +  m_copy_insn_closure = gdbarch_displaced_step_copy_insn (arch,
>>> +							  m_original_pc,
>>> +							  m_addr,
>>> +							  regcache);
>>> +  if (m_copy_insn_closure == nullptr)
>>> +    {
>>> +      /* The architecture doesn't know how or want to displaced step
>>> +        this instruction or instruction sequence.  Fallback to
>>> +        stepping over the breakpoint in-line.  */
>>> +      return DISPLACED_STEP_PREPARE_STATUS_ERROR;
>>> +    }
>>> +
>>> +  try
>>> +    {
>>> +      /* Resume execution at the copy.  */
>>> +      regcache_write_pc (regcache, m_addr);
>>> +    }
>>> +  catch (...)
>>
>> I'd rather we don't use 'catch (...)' throughout gdb.  It swallows
>> too much.  We should catch gdb_exception_error in most cases or
>> gdb_exception when we also want to catch Ctrl-C/QUIT.  If something
>> throws something else, I'd rather not swallow it, since it's most
>> probably a bug.
>>
>> I'm a bit confused on error handling here.  Before the patch, we used
>> displaced_step_reset_cleanup, but didn't swallow the error.  After the patch,
>> the exception is swallowed and DISPLACED_STEP_PREPARE_STATUS_ERROR is
>> returned.  Was that on purpose?
> 
> Hmm, I probably did this to ensure that any error would be reported by
> returning a status code, rather than some errors returning _ERROR and
> some errors throwing an exception.  So just for consistency.
> 

The function still throws errors anyway, e.g. a bit above:

  int status = target_read_memory (m_addr, m_saved_copy.data (), len);
  if (status != 0)
    throw_error (MEMORY_ERROR,
		 _("Error accessing memory address %s (%s) for "
		   "displaced-stepping scratch space."),
		 paddress (arch, m_addr), safe_strerror (status));

That's then caught by displaced_step_prepare, which results in
the "disabling displaced stepping" warning, and GDB disabling
displaced stepping for the inferior.  

Also, NOT_SUPPORTED_ERROR is handled the same way.  See:

 commit 16b4184277c4ad5b4a20278060fd3f6259d1ed49
 Author:     Pedro Alves <palves@redhat.com>
 AuthorDate: Tue Mar 15 16:33:04 2016 +0000

    Fix PR gdb/19676: Disable displaced stepping if /proc not mounted

Any other error is just propagated out, likely all the way to
the prompt:

 static displaced_step_prepare_status
 displaced_step_prepare (thread_info *thread)
 {
 ...
   catch (const gdb_exception_error &ex)
     {
      struct displaced_step_inferior_state *displaced_state;

      if (ex.error != MEMORY_ERROR
	  && ex.error != NOT_SUPPORTED_ERROR)
	throw;

> Would it make sense to remove the _ERROR status code and report all
> errors using exceptions then?

I don't think so, because there are 3 kinds of "errors":

 #1 - MEMORY and NOT_SUPPORTED errors result in disabling displaced stepping
      for the inferior permanently.

 #2 - other errors/exceptions propagate outwards.

 #3 - "prepare" returning "Can't do displace step for this instruction" for
      some reason.  Like the backend not supporting displaced stepping
      that particular instruction, or the breakpoint address falling
      within the scratch pad.  I.e., these cases:

...
   if (breakpoint_in_range_p (aspace, m_addr, len))
    {
      /* There's a breakpoint set in the scratch pad location range
	 (which is usually around the entry point).  We'd either
	 install it before resuming, which would overwrite/corrupt the
	 scratch pad, or if it was already inserted, this displaced
	 step would overwrite it.  The latter is OK in the sense that
	 we already assume that no thread is going to execute the code
	 in the scratch pad range (after initial startup) anyway, but
	 the former is unacceptable.  Simply punt and fallback to
	 stepping over this breakpoint in-line.  */
      displaced_debug_printf ("breakpoint set in scratch pad.  "
			      "Stepping over breakpoint in-line instead.");

      return DISPLACED_STEP_PREPARE_STATUS_ERROR;
    }
...
   m_copy_insn_closure = gdbarch_displaced_step_copy_insn (arch,
 							  m_original_pc,
 							  m_addr,
 							  regcache);
   if (m_copy_insn_closure == nullptr)
     {
       /* The architecture doesn't know how or want to displaced step
         this instruction or instruction sequence.  Fallback to
         stepping over the breakpoint in-line.  */
       return DISPLACED_STEP_PREPARE_STATUS_ERROR;
     }

The "failed to write pc" try/catch case seems debatable, but I'd
lean on it being case #2.

I think that case #3 calls for renaming DISPLACED_STEP_PREPARE_STATUS_ERROR
to something else to avoid confusion.  Note that in master, -1 wasn't
documented in terms of error:

...
   Returns 1 if preparing was successful -- this thread is going to be
   stepped now; 0 if displaced stepping this thread got queued; or -1
   if this instruction can't be displaced stepped.  */

 static int
 displaced_step_prepare_throw (thread_info *tp)

>>> +  /* Steal the global thread step over chain.  */
>>
>> It would be good expand the command explaining _why_ steal.
> 
> How about:
> 
>   /* Steal the global thread step over chain.  As we try to initiate displaced
>      steps, threads will be enqueued in the global chain if no buffers are
>      available.  If we iterated on the global chain directly, we might\x13 iterate
>      indefinitely.  */

Sounds good.

> 
>>> @@ -2033,7 +1945,30 @@ start_step_over (void)
>>>  	 displaced step on a thread of other process. */
>>>      }
>>>
>>> -  return false;
>>> +    /* If there are threads left in the THREADS_TO_STEP list, but we have
>>> +       detected that we can't start anything more, put back these threads
>>> +       in the global list.  */
>>
>> Do we also need to do this if an exception happens to escape the function?
>> We might end up pretty bonkers anyhow if that happens, though...
> 
> Yes, I realized that when playing with this code again yesterday.  A
> scope_exit would help for this I think.
> 
> But I'm also wondering if we should enqueue back the thread TP that was
> dequeued, that caused an error.  This one has already been dequeued from
> THREADS_TO_STEP.  For example, if thread_still_needs_step_over throws
> (it reads the PC, so that could happen), should we put back the thread
> in the global chain?  If we do, and the same error happens over and
> over, the thread will never leave the step over chain and be the first
> in line, preventing any other displaced step to happen.
> 
> If, on the other hand, the error was just an intermittent one and we
> don't put it back in the queue, the next time we'll resume the thread
> we'll realize it needs a step over and enqueue it again.  So it sounds
> less risky to me to just not enqueue back the thread on error.  I'm
> really not sure, I find it difficult to reason about all the possible
> cases.

Yeah, not enqueuing sounds OK.  And indeed, error handling in all of
infrun is very hard to reason about, and likely to end up with things
messed up, e.g., with a thread marked as running when it is really
stopped.

> 
> Also, re-reading that code, I noticed that we only return "true" when an
> inline step over is started, or if a displaced step on an all-stop
> target was started.  That's the case even before the patch.  I suppose
> that's on purpose, because the caller wants to know whether it can
> resume more stuff or not?  

Yes.  If it returns true, then the caller shouldn't resume anything
and go straight to waiting.  ISTR having some trouble coming up with
the right conditions at the callers.  Maybe it could be simplified.
I no longer remember why start_step_over returns false here:

  /* Don't start a new step-over if we already have an in-line
     step-over operation ongoing.  */
  if (step_over_info_valid_p ())
    return false;

Off hand, it seems like that should return true.  But it may
be that the intention was to only return true if anything
that was stopped before was started.  I'd need to experiment
to know better now.  :-/

> If so, I think we should update
> start_step_over's documentation:
> 
>   /* Are there any pending step-over requests?  If so, run all we can
>      now and return true.  Otherwise, return false.  */
> 

>>> @@ -5293,25 +5226,23 @@ handle_inferior_event (struct execution_control_state *ecs)
>>>        {
>>>  	struct regcache *regcache = get_thread_regcache (ecs->event_thread);
>>>  	struct gdbarch *gdbarch = regcache->arch ();
>>> +	inferior *parent_inf = find_inferior_ptid (ecs->target, ecs->ptid);
>>> +
>>> +	if (ecs->ws.kind == TARGET_WAITKIND_FORKED)
>>> +	  {
>>> +	    /* Restore in the child process any displaced stepping buffers that
>>> +	       were in use at the time of the fork.  */
>>> +	    gdbarch_displaced_step_restore_all_in_ptid
>>> +	      (gdbarch, parent_inf, ecs->ws.value.related_pid);
>>> +	  }
>>
>> Why was this moved out of the displaced_step_in_progress_thread check
>> below?
> 
> If:
> 
> 1. thread 1 is doing a displaced step
> 2. thread 2 does a fork, not letting time for thread 1 to complete
> 
> event_thread is thread 2, and it is not doing a displaced step, so we
> don't enter the
> 
>   if (displaced_step_in_progress_thread (ecs->event_thread))
> 
> But we still want to restore the bytes in the child used as the
> displaced stepping buffer for thread 1.  Does that make sense?  Is it
> possible that the current code is not correct in that regard?

Indeed, looks like it.  Good catch.  How about fixing that
in a separate patch, then, with its own commit log?

>>> +int
>>> +thread_step_over_chain_length (thread_info *tp)
>>> +{
>>> +  if (tp == nullptr)
>>> +    return 0;
>>> +
>>> +  int num = 1;
>>
>> Should we add:
>>
>>   gdb_assert (thread_is_in_step_over_chain (tp));
>>
>> ?
>>
>> But then again, it's a bit odd to allow tp == nullptr,
>> but not allow tp->step_over_next == nullptr?
> 
> Well, a pointer to an empty thread step over chain is a nullptr: when
> the global chain is empty, global_thread_step_over_chain is nullptr.
> Same for threads_to_step that is local to start_step_over.  So
> it makes sense that we can do thread_step_over_chain_length(nullptr).
> 
> However, it would be a mistake to pass a non-nullptr pointer to a thread
> that is not in a step over chain, so I think it would be a good idea to
> add the assert you propose.
> 
> I will clarify the documentation of the function:
> 
>   /* Return the length of the the step over chain TP is in.
> 
>      If TP is non-nullptr, the thread must be in a step over chain.
>      TP may be nullptr, in which case it denotes an empty list, so a length of
>      0.  */
> 

OK.

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

* Re: [PATCH 09/12] gdb: move displaced stepping logic to gdbarch, allow starting concurrent displaced steps
  2020-11-25 19:35       ` Simon Marchi
@ 2020-11-26 14:25         ` Pedro Alves
  2020-11-30 19:13           ` Simon Marchi
  0 siblings, 1 reply; 44+ messages in thread
From: Pedro Alves @ 2020-11-26 14:25 UTC (permalink / raw)
  To: Simon Marchi, Simon Marchi, gdb-patches

On 11/25/20 7:35 PM, Simon Marchi wrote:
> On 2020-11-25 2:29 p.m., Simon Marchi via Gdb-patches wrote:
>> True, changed for:
>>
>>   for (thread_info *iter = tp->step_over_next; iter != tp; iter->step_over_next)
>>     ++num;
> 
> Good thing the compiler didn't let me compile that, it would have made
> an infinite loop!  Here's the correct one:
> 
>   for (thread_info *iter = tp->step_over_next; iter != tp;
>        iter = iter->step_over_next)
>     ++num;

My personal preference is that when you need to put one of the
statements in its own line, then put all statements in their own line:

 for (thread_info *iter = tp->step_over_next; 
      iter != tp;
      iter = iter->step_over_next)
   ++num;

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

* Re: [PATCH 12/12] gdb: use two displaced step buffers on amd64/Linux
  2020-11-25 20:56         ` Simon Marchi
@ 2020-11-26 21:43           ` Simon Marchi
  2020-11-26 22:34             ` Simon Marchi
  2020-11-28 18:56             ` Pedro Alves
  0 siblings, 2 replies; 44+ messages in thread
From: Simon Marchi @ 2020-11-26 21:43 UTC (permalink / raw)
  To: Pedro Alves, Simon Marchi, gdb-patches

On 2020-11-25 3:56 p.m., Simon Marchi wrote:
> On 2020-11-25 3:07 p.m., Simon Marchi wrote:
>> We still have to figure out why the performance regresses compared to
>> master.  One thing is that we still do an extra resume attempt each
>> time.  We do one resume where the disp step prepare succeeds and one
>> more where it fails.  That's twice as much work as before, where we did
>> one successful prepare and then skipped the next ones.  I'll make an
>> experimental change such that the arch can say "the prepare worked, but
>> now I'm out of buffer", just to see how that improves (or not)
>> performance.
>
> That made just a small difference.  Let's call this new patch E:
>
>  #9 + A + B + C + D + E: 105,727

Ok, I played a bit more with this and found a case where we do more
work that may explain the difference.

handle_signal_stop calls finish_step_over, which calls start_step_over.
Let's say that the displaced step buffer is used by some thread and
another thread either hits the breakpoint, we go into start_step_over to
see if we can start a new step over.  We can't, because the event thread
isn't the one that is using the displaced stepping buffer, the buffer is
still occupied.

In the pre-patch code, that early check in start_step_over:

      /* If this inferior already has a displaced step in process,
	 don't start a new one.  */
      if (displaced_step_in_progress (tp->inf))
	continue;

means that we won't even try one resumption, we'll skip over all threads
in the chain (the only cost is the cost of iteration).

In the patch I previously called "C", start_step_over starts with the
assumption that the inferior has an available buffer, and needs to do at
least one resumption attempt that returns "unavailable" before it
assumes there are no more available.  That one unnecessary resumption
attempt happens over and over and ends up being costly.

I changed my patch C to make the "unavailable" property persistent,
instead of just using it locally in start_step_over.  If a displaced
step prepare returns "unavailable", we mark that this inferior has no
more buffers available, and only reset this flag when a displaced step
for this inferior finishes.  Doing so, when a thread hits the breakpoint
and start_step_over is called, the unavailable flag is already set.
Last time we tried to prepare a displaced step, there were no buffers
available.  And if no displaced step finished for this inferior since,
then why bother asking?  As a result, we don't even try one resume if
the buffers are already take, just like with the pre-patch code.

I re-did some tests with the new patch C:

master:             110,736
9:                   19,973
9 + A + B + C:      107,711
9 + A + B + C + E:  111,135
Everything up to
 using two displaced
 stepping buffers:  110,697

Note that I dropped patch D, which was to break out of the
start_step_over loop when getting "unavailable".  It didn't help much
and wasn't desired functionality anyway.

And for the reminder, patch E is that when returning "_OK", the arch can
also tell us "that was the last one, any more request would return
unavailable".  So that allows to do just one resumption attempt (at
most) per start_step_over invocation, rather than 2.

What this shows is that by using everything up to patch E, we are pretty
much on par with the performance, but with the more flexible
architecture.  That makes sense, as (I think) we do pretty much the same
work.

Re-thinking about patch C and E, I'm thinking that we could let the
architecture's displaced stepping implementation decide when it sets the
"unavailable" flag, instead of the core of GDB setting it.  The
architecture would then choose the policy it wants to adopt:

- set the flag when it used its last buffer (equivalent to patch E)
- set the flag when it returns "_UNAVAILABLE" (equivalent to patch C')
- never set the unavavaible flag (equivalent to patch 9)

It also shows that using two buffers on x86-64 doesn't improve
performance (unless there's another unfortunate bottleneck I haven't
found).  But that's what I expected.  My hypothesis is that the actual
step portion of the displaced step is so fast that we never really use
the buffers in parallel.  If we start two displaced steps, by the time
we prepare the second one, the first one has already finished and is
waiting for GDB to clean it up.  If for some reason we could displaced
step an instruction that took, say, 1 second to execute, then having
many displaced step buffers would be useful, because GDB would be able
to start them all and have them really run in parallel.

I'll prepare a user branch with all those fixup patches so you can take
a look, and if you think it makes sense I'll be ready for a v2.

Simon

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

* Re: [PATCH 12/12] gdb: use two displaced step buffers on amd64/Linux
  2020-11-26 21:43           ` Simon Marchi
@ 2020-11-26 22:34             ` Simon Marchi
  2020-11-28 18:56             ` Pedro Alves
  1 sibling, 0 replies; 44+ messages in thread
From: Simon Marchi @ 2020-11-26 22:34 UTC (permalink / raw)
  To: Pedro Alves, Simon Marchi, gdb-patches


On 2020-11-26 4:43 p.m., Simon Marchi wrote:
> I'll prepare a user branch with all those fixup patches so you can take
> a look, and if you think it makes sense I'll be ready for a v2.

Ok, I pushed the branch with the performance fixes on Sourceware, branch
users/simark/concurrent-displaced-step-1-with-perf-fixes.

I merged patches C and E into the idea I mentioned in my last message,
where it's the gdbarch that sets and resets the unavailable flag
whenever it wants to.

And note that I haven't addressed the error reporting issues in patch 9 yet.

Simon

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

* Re: [PATCH 12/12] gdb: use two displaced step buffers on amd64/Linux
  2020-11-26 21:43           ` Simon Marchi
  2020-11-26 22:34             ` Simon Marchi
@ 2020-11-28 18:56             ` Pedro Alves
  1 sibling, 0 replies; 44+ messages in thread
From: Pedro Alves @ 2020-11-28 18:56 UTC (permalink / raw)
  To: Simon Marchi, Simon Marchi, gdb-patches

On 11/26/20 9:43 PM, Simon Marchi wrote:
> On 2020-11-25 3:56 p.m., Simon Marchi wrote:
>> On 2020-11-25 3:07 p.m., Simon Marchi wrote:
>>> We still have to figure out why the performance regresses compared to
>>> master.  One thing is that we still do an extra resume attempt each
>>> time.  We do one resume where the disp step prepare succeeds and one
>>> more where it fails.  That's twice as much work as before, where we did
>>> one successful prepare and then skipped the next ones.  I'll make an
>>> experimental change such that the arch can say "the prepare worked, but
>>> now I'm out of buffer", just to see how that improves (or not)
>>> performance.
>>
>> That made just a small difference.  Let's call this new patch E:
>>
>>  #9 + A + B + C + D + E: 105,727
> 
> Ok, I played a bit more with this and found a case where we do more
> work that may explain the difference.
> 
> handle_signal_stop calls finish_step_over, which calls start_step_over.
> Let's say that the displaced step buffer is used by some thread and
> another thread either hits the breakpoint, we go into start_step_over to
> see if we can start a new step over.  We can't, because the event thread
> isn't the one that is using the displaced stepping buffer, the buffer is
> still occupied.
> 
> In the pre-patch code, that early check in start_step_over:
> 
>       /* If this inferior already has a displaced step in process,
> 	 don't start a new one.  */
>       if (displaced_step_in_progress (tp->inf))
> 	continue;
> 
> means that we won't even try one resumption, we'll skip over all threads
> in the chain (the only cost is the cost of iteration).
> 
> In the patch I previously called "C", start_step_over starts with the
> assumption that the inferior has an available buffer, and needs to do at
> least one resumption attempt that returns "unavailable" before it
> assumes there are no more available.  That one unnecessary resumption
> attempt happens over and over and ends up being costly.
> 
> I changed my patch C to make the "unavailable" property persistent,
> instead of just using it locally in start_step_over.  If a displaced
> step prepare returns "unavailable", we mark that this inferior has no
> more buffers available, and only reset this flag when a displaced step
> for this inferior finishes.  Doing so, when a thread hits the breakpoint
> and start_step_over is called, the unavailable flag is already set.
> Last time we tried to prepare a displaced step, there were no buffers
> available.  And if no displaced step finished for this inferior since,
> then why bother asking?  As a result, we don't even try one resume if
> the buffers are already take, just like with the pre-patch code.
> 
> I re-did some tests with the new patch C:
> 
> master:             110,736
> 9:                   19,973
> 9 + A + B + C:      107,711
> 9 + A + B + C + E:  111,135
> Everything up to
>  using two displaced
>  stepping buffers:  110,697
> 
> Note that I dropped patch D, which was to break out of the
> start_step_over loop when getting "unavailable".  It didn't help much
> and wasn't desired functionality anyway.
> 
> And for the reminder, patch E is that when returning "_OK", the arch can
> also tell us "that was the last one, any more request would return
> unavailable".  So that allows to do just one resumption attempt (at
> most) per start_step_over invocation, rather than 2.
> 
> What this shows is that by using everything up to patch E, we are pretty
> much on par with the performance, but with the more flexible
> architecture.  That makes sense, as (I think) we do pretty much the same
> work.
> 
> Re-thinking about patch C and E, I'm thinking that we could let the
> architecture's displaced stepping implementation decide when it sets the
> "unavailable" flag, instead of the core of GDB setting it.  The
> architecture would then choose the policy it wants to adopt:
> 
> - set the flag when it used its last buffer (equivalent to patch E)
> - set the flag when it returns "_UNAVAILABLE" (equivalent to patch C')
> - never set the unavavaible flag (equivalent to patch 9)
> 
> It also shows that using two buffers on x86-64 doesn't improve
> performance (unless there's another unfortunate bottleneck I haven't
> found).  But that's what I expected.  My hypothesis is that the actual
> step portion of the displaced step is so fast that we never really use
> the buffers in parallel.  If we start two displaced steps, by the time
> we prepare the second one, the first one has already finished and is
> waiting for GDB to clean it up.  If for some reason we could displaced
> step an instruction that took, say, 1 second to execute, then having
> many displaced step buffers would be useful, because GDB would be able
> to start them all and have them really run in parallel.
> 
> I'll prepare a user branch with all those fixup patches so you can take
> a look, and if you think it makes sense I'll be ready for a v2.

I took a look and your branch, and it makes sense to me.
I run the perf test on my end, and the performance gap is practically
gone on my end as well.  I'm happy with that.

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

* Re: [PATCH 11/12] gdb: make displaced stepping implementation capable of managing multiple buffers
  2020-11-25  1:41   ` Pedro Alves
@ 2020-11-30 18:58     ` Simon Marchi
  2020-11-30 19:01     ` Simon Marchi
  1 sibling, 0 replies; 44+ messages in thread
From: Simon Marchi @ 2020-11-30 18:58 UTC (permalink / raw)
  To: Pedro Alves, Simon Marchi, gdb-patches

On 2020-11-24 8:41 p.m., Pedro Alves wrote:
> Call:
>
>       m_buffers.reserve (buffer_addrs.size ());
>
> before.
>
>
> But even better, the caller is allocating a temporary std::vector
> and passing that:
>
>  +      std::vector<CORE_ADDR> buffers;
>  +      for (int i = 0; i < gdbarch_data->num_disp_step_buffers; i++)
>  +       buffers.push_back (disp_step_buf_addr + i * buf_len);
>
> So how about making the ctor above be instead:
>
>   displaced_step_buffers (std::vector<CORE_ADDR> &&buffer_addrs);
>
> and make the caller move the vector into displaced_step_buffers?
>
>  +      per_inferior->disp_step_bufs.emplace (std::move (buffers));

displaced_step_buffers' ctor takes a vector of CORE_ADDR, but it
contains a vector of displaced_step_buffer.  So it can't just move and
use directly the vector passed by its caller.

I'll call reserve though.

Simon

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

* Re: [PATCH 11/12] gdb: make displaced stepping implementation capable of managing multiple buffers
  2020-11-25  1:41   ` Pedro Alves
  2020-11-30 18:58     ` Simon Marchi
@ 2020-11-30 19:01     ` Simon Marchi
  1 sibling, 0 replies; 44+ messages in thread
From: Simon Marchi @ 2020-11-30 19:01 UTC (permalink / raw)
  To: Pedro Alves, Simon Marchi, gdb-patches

On 2020-11-24 8:41 p.m., Pedro Alves wrote:
> A few minor comments below.  Otherwise LGTM.

I forgot to reply to the other comments.

> On 11/10/20 9:46 PM, Simon Marchi via Gdb-patches wrote:
>> -      return DISPLACED_STEP_PREPARE_STATUS_ERROR;
>> +  for (displaced_step_buffer &candidate : m_buffers)
>> +    {
>> +      bool bp_in_range = breakpoint_in_range_p (aspace, candidate.addr, len);
>> +      bool is_free = candidate.current_thread == nullptr;
>> +
>> +      if (!bp_in_range)
>> +        {
>> +          if (is_free)
>
> tabs vs spaces.

Done.

>> +	    {
>> +	      buffer = &candidate;
>> +	      break;
>> +	    }
>> +	  else
>> +	    {
>> +	      /* This buffer would be suitable, but it's used right now.  */
>> +	      fail_status = DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE;
>> +	    }
>> +        }
>
> tabs vs spaces.

Done.

>> +  struct displaced_step_buffer
>> +  {
>> +    displaced_step_buffer (CORE_ADDR addr)
>
> "explicit".

Done.

>
>> +      : addr (addr)
>> +    {}
>> +
>> +    const CORE_ADDR addr;
>> +
>> +    /* The original PC of the instruction currently begin stepped.  */
>
> "begin" -> "being" ?

Indeed.

Thanks,

Simon

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

* Re: [PATCH 09/12] gdb: move displaced stepping logic to gdbarch, allow starting concurrent displaced steps
  2020-11-26 14:25         ` Pedro Alves
@ 2020-11-30 19:13           ` Simon Marchi
  0 siblings, 0 replies; 44+ messages in thread
From: Simon Marchi @ 2020-11-30 19:13 UTC (permalink / raw)
  To: Pedro Alves, Simon Marchi, gdb-patches

On 2020-11-26 9:25 a.m., Pedro Alves wrote:
> My personal preference is that when you need to put one of the
> statements in its own line, then put all statements in their own line:
> 
>  for (thread_info *iter = tp->step_over_next; 
>       iter != tp;
>       iter = iter->step_over_next)
>    ++num;

Ack.

Simon

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

* Re: [PATCH 09/12] gdb: move displaced stepping logic to gdbarch, allow starting concurrent displaced steps
  2020-11-26 14:24       ` Pedro Alves
@ 2020-11-30 20:26         ` Simon Marchi
  0 siblings, 0 replies; 44+ messages in thread
From: Simon Marchi @ 2020-11-30 20:26 UTC (permalink / raw)
  To: Pedro Alves, Simon Marchi, gdb-patches

On 2020-11-26 9:24 a.m., Pedro Alves wrote:
> The function still throws errors anyway, e.g. a bit above:
>
>   int status = target_read_memory (m_addr, m_saved_copy.data (), len);
>   if (status != 0)
>     throw_error (MEMORY_ERROR,
> 		 _("Error accessing memory address %s (%s) for "
> 		   "displaced-stepping scratch space."),
> 		 paddress (arch, m_addr), safe_strerror (status));
>
> That's then caught by displaced_step_prepare, which results in
> the "disabling displaced stepping" warning, and GDB disabling
> displaced stepping for the inferior.
>
> Also, NOT_SUPPORTED_ERROR is handled the same way.  See:
>
>  commit 16b4184277c4ad5b4a20278060fd3f6259d1ed49
>  Author:     Pedro Alves <palves@redhat.com>
>  AuthorDate: Tue Mar 15 16:33:04 2016 +0000
>
>     Fix PR gdb/19676: Disable displaced stepping if /proc not mounted
>
> Any other error is just propagated out, likely all the way to
> the prompt:
>
>  static displaced_step_prepare_status
>  displaced_step_prepare (thread_info *thread)
>  {
>  ...
>    catch (const gdb_exception_error &ex)
>      {
>       struct displaced_step_inferior_state *displaced_state;
>
>       if (ex.error != MEMORY_ERROR
> 	  && ex.error != NOT_SUPPORTED_ERROR)
> 	throw;
>
>> Would it make sense to remove the _ERROR status code and report all
>> errors using exceptions then?
>
> I don't think so, because there are 3 kinds of "errors":
>
>  #1 - MEMORY and NOT_SUPPORTED errors result in disabling displaced stepping
>       for the inferior permanently.
>
>  #2 - other errors/exceptions propagate outwards.
>
>  #3 - "prepare" returning "Can't do displace step for this instruction" for
>       some reason.  Like the backend not supporting displaced stepping
>       that particular instruction, or the breakpoint address falling
>       within the scratch pad.  I.e., these cases:
>
> ...
>    if (breakpoint_in_range_p (aspace, m_addr, len))
>     {
>       /* There's a breakpoint set in the scratch pad location range
> 	 (which is usually around the entry point).  We'd either
> 	 install it before resuming, which would overwrite/corrupt the
> 	 scratch pad, or if it was already inserted, this displaced
> 	 step would overwrite it.  The latter is OK in the sense that
> 	 we already assume that no thread is going to execute the code
> 	 in the scratch pad range (after initial startup) anyway, but
> 	 the former is unacceptable.  Simply punt and fallback to
> 	 stepping over this breakpoint in-line.  */
>       displaced_debug_printf ("breakpoint set in scratch pad.  "
> 			      "Stepping over breakpoint in-line instead.");
>
>       return DISPLACED_STEP_PREPARE_STATUS_ERROR;
>     }
> ...
>    m_copy_insn_closure = gdbarch_displaced_step_copy_insn (arch,
>  							  m_original_pc,
>  							  m_addr,
>  							  regcache);
>    if (m_copy_insn_closure == nullptr)
>      {
>        /* The architecture doesn't know how or want to displaced step
>          this instruction or instruction sequence.  Fallback to
>          stepping over the breakpoint in-line.  */
>        return DISPLACED_STEP_PREPARE_STATUS_ERROR;
>      }
>
> The "failed to write pc" try/catch case seems debatable, but I'd
> lean on it being case #2.
>
> I think that case #3 calls for renaming DISPLACED_STEP_PREPARE_STATUS_ERROR
> to something else to avoid confusion.  Note that in master, -1 wasn't
> documented in terms of error:
>
> ...
>    Returns 1 if preparing was successful -- this thread is going to be
>    stepped now; 0 if displaced stepping this thread got queued; or -1
>    if this instruction can't be displaced stepped.  */
>
>  static int
>  displaced_step_prepare_throw (thread_info *tp)

Ok, thanks for this explanation.  I didn't understand well the different
error modes (can't displaced-step this particular instruction vs some
other unexpected error) and how they were handled.

I'll rename DISPLACED_STEP_PREPARE_STATUS_ERROR to
DISPLACED_STEP_PREPARE_STATUS_CANT.  It fits well with the comments in
the code where it is used, that all say variants of "can't displaced
step this instruction, fall back on in-line stepping".

>>>> -  return false;
>>>> +    /* If there are threads left in the THREADS_TO_STEP list, but we have
>>>> +       detected that we can't start anything more, put back these threads
>>>> +       in the global list.  */
>>>
>>> Do we also need to do this if an exception happens to escape the function?
>>> We might end up pretty bonkers anyhow if that happens, though...
>>
>> Yes, I realized that when playing with this code again yesterday.  A
>> scope_exit would help for this I think.
>>
>> But I'm also wondering if we should enqueue back the thread TP that was
>> dequeued, that caused an error.  This one has already been dequeued from
>> THREADS_TO_STEP.  For example, if thread_still_needs_step_over throws
>> (it reads the PC, so that could happen), should we put back the thread
>> in the global chain?  If we do, and the same error happens over and
>> over, the thread will never leave the step over chain and be the first
>> in line, preventing any other displaced step to happen.
>>
>> If, on the other hand, the error was just an intermittent one and we
>> don't put it back in the queue, the next time we'll resume the thread
>> we'll realize it needs a step over and enqueue it again.  So it sounds
>> less risky to me to just not enqueue back the thread on error.  I'm
>> really not sure, I find it difficult to reason about all the possible
>> cases.
>
> Yeah, not enqueuing sounds OK.  And indeed, error handling in all of
> infrun is very hard to reason about, and likely to end up with things
> messed up, e.g., with a thread marked as running when it is really
> stopped.

Ok, did that.

>> Also, re-reading that code, I noticed that we only return "true" when an
>> inline step over is started, or if a displaced step on an all-stop
>> target was started.  That's the case even before the patch.  I suppose
>> that's on purpose, because the caller wants to know whether it can
>> resume more stuff or not?
>
> Yes.  If it returns true, then the caller shouldn't resume anything
> and go straight to waiting.  ISTR having some trouble coming up with
> the right conditions at the callers.  Maybe it could be simplified.
> I no longer remember why start_step_over returns false here:
>
>   /* Don't start a new step-over if we already have an in-line
>      step-over operation ongoing.  */
>   if (step_over_info_valid_p ())
>     return false;
>
> Off hand, it seems like that should return true.  But it may
> be that the intention was to only return true if anything
> that was stopped before was started.  I'd need to experiment
> to know better now.  :-/

Ok, I'll leave it as-is for now and treat is as a separete issue.

>>>> @@ -5293,25 +5226,23 @@ handle_inferior_event (struct execution_control_state *ecs)
>>>>        {
>>>>  	struct regcache *regcache = get_thread_regcache (ecs->event_thread);
>>>>  	struct gdbarch *gdbarch = regcache->arch ();
>>>> +	inferior *parent_inf = find_inferior_ptid (ecs->target, ecs->ptid);
>>>> +
>>>> +	if (ecs->ws.kind == TARGET_WAITKIND_FORKED)
>>>> +	  {
>>>> +	    /* Restore in the child process any displaced stepping buffers that
>>>> +	       were in use at the time of the fork.  */
>>>> +	    gdbarch_displaced_step_restore_all_in_ptid
>>>> +	      (gdbarch, parent_inf, ecs->ws.value.related_pid);
>>>> +	  }
>>>
>>> Why was this moved out of the displaced_step_in_progress_thread check
>>> below?
>>
>> If:
>>
>> 1. thread 1 is doing a displaced step
>> 2. thread 2 does a fork, not letting time for thread 1 to complete
>>
>> event_thread is thread 2, and it is not doing a displaced step, so we
>> don't enter the
>>
>>   if (displaced_step_in_progress_thread (ecs->event_thread))
>>
>> But we still want to restore the bytes in the child used as the
>> displaced stepping buffer for thread 1.  Does that make sense?  Is it
>> possible that the current code is not correct in that regard?
>
> Indeed, looks like it.  Good catch.  How about fixing that
> in a separate patch, then, with its own commit log?

Will do.  I didn't do a separate patch at first because I initially
thought: "now that we can do multiple displaced steps in parallel, it
can be that the other displaced steps does a fork!".  I didn't think
right away of the case where it's a non-displaced-stepping thread who
does a fork.

Simon

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

* Re: [PATCH 02/12] gdb: clear inferior displaced stepping state on exec
  2020-11-25  1:28   ` Pedro Alves
@ 2020-12-01  4:27     ` Simon Marchi
  0 siblings, 0 replies; 44+ messages in thread
From: Simon Marchi @ 2020-12-01  4:27 UTC (permalink / raw)
  To: Pedro Alves, Simon Marchi, gdb-patches


On 2020-11-24 8:28 p.m., Pedro Alves wrote:
>> I tried to write a test where a non-main thread displaced-steps an exec
>> syscall, where things would hang due to the displaced step buffer not
>> getting released.  However, due to PR 26754 [1], it is hard to make it
>> stable.  So I'm not including a test for this patch.  If you have an
>> idea for another way to test this without triggering this bug, I'd like
>> to hear it.
>>
>> [1] https://sourceware.org/bugzilla/show_bug.cgi?id=26754
> I can't think of another way to test this.
>

We can simply put a breakpoint with an always false condition on the
syscall instruction and run from start.  The exec will be displaced
stepped while the main thread will run.  I can reliably reproduce the
hang this way.

Simon

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

end of thread, other threads:[~2020-12-01  4:27 UTC | newest]

Thread overview: 44+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-10 21:46 [PATCH 00/12] Concurrent displaced stepping Simon Marchi
2020-11-10 21:46 ` [PATCH 01/12] gdb: add inferior_execd observable Simon Marchi
2020-11-25  1:28   ` Pedro Alves
2020-11-10 21:46 ` [PATCH 02/12] gdb: clear inferior displaced stepping state on exec Simon Marchi
2020-11-25  1:28   ` Pedro Alves
2020-12-01  4:27     ` Simon Marchi
2020-11-10 21:46 ` [PATCH 03/12] gdb: rename things related to step over chains Simon Marchi
2020-11-25  1:28   ` Pedro Alves
2020-11-25 13:16     ` Simon Marchi
2020-11-10 21:46 ` [PATCH 04/12] gdb: rename displaced_step_closure to displaced_step_copy_insn_closure Simon Marchi
2020-11-25  1:29   ` Pedro Alves
2020-11-10 21:46 ` [PATCH 05/12] gdb: rename displaced_step_fixup to displaced_step_finish Simon Marchi
2020-11-25  1:29   ` Pedro Alves
2020-11-10 21:46 ` [PATCH 06/12] gdb: introduce status enum for displaced step prepare/finish Simon Marchi
2020-11-11 23:36   ` Andrew Burgess
2020-11-25 13:17     ` Simon Marchi
2020-11-25  1:30   ` Pedro Alves
2020-11-25 13:20     ` Simon Marchi
2020-11-10 21:46 ` [PATCH 07/12] gdb: pass inferior to get_linux_inferior_data Simon Marchi
2020-11-25  1:30   ` Pedro Alves
2020-11-10 21:46 ` [PATCH 08/12] gdb: move displaced stepping types to displaced-stepping.{h, c} Simon Marchi
2020-11-25  1:30   ` Pedro Alves
2020-11-10 21:46 ` [PATCH 09/12] gdb: move displaced stepping logic to gdbarch, allow starting concurrent displaced steps Simon Marchi
2020-11-25  1:40   ` Pedro Alves
2020-11-25 19:29     ` Simon Marchi
2020-11-25 19:35       ` Simon Marchi
2020-11-26 14:25         ` Pedro Alves
2020-11-30 19:13           ` Simon Marchi
2020-11-26 14:24       ` Pedro Alves
2020-11-30 20:26         ` Simon Marchi
2020-11-10 21:46 ` [PATCH 10/12] gdb: change linux gdbarch data from post to pre-init Simon Marchi
2020-11-25  1:41   ` Pedro Alves
2020-11-10 21:46 ` [PATCH 11/12] gdb: make displaced stepping implementation capable of managing multiple buffers Simon Marchi
2020-11-25  1:41   ` Pedro Alves
2020-11-30 18:58     ` Simon Marchi
2020-11-30 19:01     ` Simon Marchi
2020-11-10 21:46 ` [PATCH 12/12] gdb: use two displaced step buffers on amd64/Linux Simon Marchi
2020-11-25  1:42   ` Pedro Alves
2020-11-25  6:26     ` Simon Marchi
2020-11-25 20:07       ` Simon Marchi
2020-11-25 20:56         ` Simon Marchi
2020-11-26 21:43           ` Simon Marchi
2020-11-26 22:34             ` Simon Marchi
2020-11-28 18:56             ` Pedro Alves

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).